Example usage for android.graphics Rect Rect

List of usage examples for android.graphics Rect Rect

Introduction

In this page you can find the example usage for android.graphics Rect Rect.

Prototype

public Rect(int left, int top, int right, int bottom) 

Source Link

Document

Create a new rectangle with the specified coordinates.

Usage

From source file:self.philbrown.droidQuery.$.java

/**
 * Adds an Image over each selected View as a mask.
 * In most cases, this mask can be retrieved by querying siblings. For example:
 * <pre>/* ww w.j a va 2  s  .c  o m*/
 * ImageView mask = (ImageView) $.with(myView).parent().selectChildren().selectImages().view(0);
 * </pre>
 * @param source asset path, file path (starting with "file://") or URL to image
 * @param width specifies the output bitmap width
 * @param height specifies the output bitmap height
 * @param error if the given source is a file or asset, this receives a droidQuery wrapping the 
 * current context and the {@code Throwable} error. Otherwise, this will receive an
 * Ajax error.
 * @return this
 * @see AjaxOptions#error(Function)
 */
public $ mask(String source, int width, int height, Function error) {
    if (source.startsWith("file://")) {
        try {
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
            if (width >= 0)
                opt.outWidth = width;
            if (height >= 0)
                opt.outHeight = height;
            Bitmap bitmap = BitmapFactory.decodeFile(source.substring(6), opt);
            for (View v : views) {
                ImageView image = new ImageView(context);
                image.setImageBitmap(Bitmap.createBitmap(bitmap));
                image.setScaleType(ScaleType.FIT_XY);
                ViewParent parent = v.getParent();
                if (parent != null && parent instanceof ViewGroup) {
                    image.setLayoutParams(v.getLayoutParams());
                    ((ViewGroup) parent).addView(image);
                } else if (v instanceof ViewGroup) {
                    image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
                    ((ViewGroup) v).addView(image);
                }
            }
        } catch (Throwable t) {
            if (error != null) {
                error.invoke($.with(context), t);
            }
        }
    } else if (URLUtil.isValidUrl(source)) {
        AjaxOptions options = new AjaxOptions().url(source).type("GET").dataType("image").context(context)
                .global(false).success(new Function() {
                    @Override
                    public void invoke($ droidQuery, Object... params) {
                        Bitmap bitmap = (Bitmap) params[0];
                        for (View v : views) {
                            ImageView image = new ImageView(context);
                            image.setImageBitmap(Bitmap.createBitmap(bitmap));
                            image.setScaleType(ScaleType.FIT_XY);
                            ViewParent parent = v.getParent();
                            if (parent != null && parent instanceof ViewGroup) {
                                image.setLayoutParams(v.getLayoutParams());
                                ((ViewGroup) parent).addView(image);
                            } else if (v instanceof ViewGroup) {
                                image.setLayoutParams(
                                        new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                ViewGroup.LayoutParams.MATCH_PARENT));
                                ((ViewGroup) v).addView(image);
                            }
                        }
                    }
                });

        if (error != null) {
            options.error(error);
        }
        if (width >= 0) {
            options.imageWidth(width);
        }
        if (height >= 0) {
            options.imageHeight(height);
        }
        $.ajax(options);
    } else {
        try {
            BitmapFactory.Options opt = new BitmapFactory.Options();
            opt.inSampleSize = 1;
            opt.inPurgeable = true;
            opt.inInputShareable = false;
            if (width >= 0)
                opt.outWidth = width;
            if (height >= 0)
                opt.outHeight = height;
            Bitmap bitmap = BitmapFactory.decodeStream(context.getAssets().open(source), new Rect(0, 0, 0, 0),
                    opt);
            for (View v : views) {
                ImageView image = new ImageView(context);
                image.setImageBitmap(Bitmap.createBitmap(bitmap));
                image.setScaleType(ScaleType.FIT_XY);
                ViewParent parent = v.getParent();
                if (parent != null && parent instanceof ViewGroup) {
                    image.setLayoutParams(v.getLayoutParams());
                    ((ViewGroup) parent).addView(image);
                } else if (v instanceof ViewGroup) {
                    image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT));
                    ((ViewGroup) v).addView(image);
                }
            }

        } catch (Throwable t) {
            if (error != null) {
                error.invoke($.with(context), t);
            }
        }

    }
    return this;
}

From source file:me.ububble.speakall.fragment.ConversationGroupFragment.java

@Override
public boolean onTouch(View v, MotionEvent event) {

    int action = event.getActionMasked();
    switch (v.getId()) {
    case R.id.record_audio:
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            textRecordingPress.setVisibility(View.INVISIBLE);
            rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            fechaAudioMillis = Calendar.getInstance().getTimeInMillis();
            File directory = new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpeakOn/Audio/Sent");
            directory.mkdirs();//from   w  ww  .jav  a2s. c o  m
            ficheroAudio = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpeakOn/Audio/Sent/"
                    + fechaAudioMillis + ".mp4";
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setOutputFile(ficheroAudio);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
            }
            mediaRecorder.start();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    finalTime += 1000;
                    int seconds = (int) (finalTime / 1000) % 60;
                    int minutes = (int) ((finalTime / (1000 * 60)) % 60);
                    int hours = (int) ((finalTime / (1000 * 60 * 60)) % 24);
                    timeFinal = String.format("%02d", minutes) + ":" + String.format("%02d", seconds);
                    timerAudio.setText(timeFinal);
                    if (!saveAudio) {
                        textRecording.setText(timeFinal);
                    }
                    handler.postDelayed(this, 1000);
                }
            });
            scaleAnimX = ObjectAnimator.ofFloat(recordAudioButton, "scaleX", 1, 1.2f);
            scaleAnimX.setDuration(800);
            scaleAnimX.setRepeatCount(ValueAnimator.INFINITE);
            scaleAnimX.setRepeatMode(ValueAnimator.REVERSE);
            scaleAnimY = ObjectAnimator.ofFloat(recordAudioButton, "scaleY", 1, 1.2f);
            scaleAnimY.setDuration(800);
            scaleAnimY.setRepeatCount(ValueAnimator.INFINITE);
            scaleAnimY.setRepeatMode(ValueAnimator.REVERSE);
            scaleAnimY.start();
            scaleAnimX.start();

            scaleRedBackgroundX = ObjectAnimator.ofFloat(audioRecordBackground, "scaleX", 1, 100f);
            scaleRedBackgroundX.setDuration(200);
            scaleRedBackgroundY = ObjectAnimator.ofFloat(audioRecordBackground, "scaleY", 1, 100f);
            scaleRedBackgroundY.setDuration(200);
            saveAudio = true;
            break;

        case MotionEvent.ACTION_MOVE:
            if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
                recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_white);
                timerAudio.setVisibility(View.GONE);
                textRecording.setTextColor(getResources().getColor(R.color.speak_all_red));
                if (!animatorBackground) {
                    textRecording.setText(timeFinal);
                    if (scaleRedBackgroundX.isRunning())
                        scaleRedBackgroundX.end();
                    if (scaleRedBackgroundY.isRunning())
                        scaleRedBackgroundY.end();
                    scaleRedBackgroundY.start();
                    scaleRedBackgroundX.start();
                    animatorBackground = true;
                }
                saveAudio = false;
            } else {
                recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_red);
                textRecording.setTextColor(getResources().getColor(R.color.speak_all_white));
                textRecording.setText(getString(R.string.audio_record));
                timerAudio.setVisibility(View.VISIBLE);
                if (animatorBackground) {
                    if (scaleRedBackgroundX.isRunning())
                        scaleRedBackgroundX.end();
                    if (scaleRedBackgroundY.isRunning())
                        scaleRedBackgroundY.end();
                    ViewHelper.setScaleX(audioRecordBackground, 1);
                    ViewHelper.setScaleY(audioRecordBackground, 1);
                    animatorBackground = false;
                }
                saveAudio = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            textRecordingPress.setVisibility(View.VISIBLE);
            animatorBackground = false;
            if (scaleAnimY.isRunning())
                scaleAnimY.end();
            if (scaleAnimX.isRunning())
                scaleAnimX.end();
            if (scaleRedBackgroundX.isRunning())
                scaleRedBackgroundX.end();
            if (scaleRedBackgroundY.isRunning())
                scaleRedBackgroundY.end();
            ViewHelper.setScaleX(audioRecordBackground, 1);
            ViewHelper.setScaleY(audioRecordBackground, 1);
            ViewHelper.setScaleX(recordAudioButton, 1);
            ViewHelper.setScaleY(recordAudioButton, 1);
            handler.removeCallbacksAndMessages(null);
            timerAudio.setText("00:00");
            timerAudio.setVisibility(View.VISIBLE);
            textRecording.setTextColor(getResources().getColor(R.color.speak_all_white));
            textRecording.setText(getString(R.string.audio_record));
            recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_red);
            if (finalTime > 1000) {
                mediaRecorder.stop();
                mediaRecorder.release();
            } else {
                saveAudio = false;
                mediaRecorder.release();
            }
            finalTime = 0;
            timeFinal = "";
            if (saveAudio) {
                try {
                    hideKeyBoard();
                    final MsgGroups msjAudio = new MsgGroups();
                    JSONArray targets = new JSONArray();
                    JSONArray contactos = new JSONArray(grupo.targets);
                    String contactosId = null;
                    if (SpeakSocket.mSocket != null) {
                        if (SpeakSocket.mSocket.connected()) {
                            for (int i = 0; i < contactos.length(); i++) {
                                JSONObject contacto = contactos.getJSONObject(i);
                                JSONObject newContact = new JSONObject();
                                Contact contact = new Select().from(Contact.class)
                                        .where("id_contact = ?", contacto.getString("name")).executeSingle();
                                if (contact != null) {
                                    if (!contact.idContacto.equals(u.id)) {
                                        if (translate)
                                            newContact.put("lang", contact.lang);
                                        else
                                            newContact.put("lang", u.lang);
                                        newContact.put("name", contact.idContacto);
                                        newContact.put("screen_name", contact.screenName);
                                        newContact.put("status", 1);
                                        contactosId += contact.idContacto;
                                        targets.put(targets.length(), newContact);
                                    }
                                }
                            }
                        } else {
                            for (int i = 0; i < contactos.length(); i++) {
                                JSONObject contacto = contactos.getJSONObject(i);
                                JSONObject newContact = new JSONObject();
                                Contact contact = new Select().from(Contact.class)
                                        .where("id_contact = ?", contacto.getString("name")).executeSingle();
                                if (contact != null) {
                                    if (!contact.idContacto.equals(u.id)) {
                                        if (translate)
                                            newContact.put("lang", contact.lang);
                                        else
                                            newContact.put("lang", u.lang);
                                        newContact.put("name", contact.idContacto);
                                        newContact.put("screen_name", contact.screenName);
                                        newContact.put("status", -1);
                                        contactosId += contact.idContacto;
                                        targets.put(targets.length(), newContact);
                                    }
                                }
                            }
                        }
                    }
                    msjAudio.grupoId = grupo.grupoId;
                    msjAudio.mensajeId = u.id + grupo.grupoId + fechaAudioMillis + Settings.Secure
                            .getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
                    msjAudio.emisor = u.id;
                    msjAudio.receptores = targets.toString();
                    msjAudio.mensaje = "new Audio";
                    msjAudio.emisorEmail = u.email;
                    msjAudio.emisorLang = u.lang;
                    msjAudio.translation = false;
                    msjAudio.emitedAt = fechaAudioMillis;
                    msjAudio.tipo = Integer.parseInt(getString(R.string.MSG_TYPE_GROUP_AUDIO));
                    msjAudio.delay = 0;
                    msjAudio.fileUploaded = false;
                    msjAudio.audioName = ficheroAudio;
                    msjAudio.save();
                    showNewMessage(msjAudio);

                } catch (Exception e) {
                    // TODO: handle exception
                }
            } else {
                new File(ficheroAudio).delete();
            }
            break;
        }
        break;
    }

    return true;
}

From source file:me.ububble.speakall.fragment.ConversationChatFragment.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    int action = event.getActionMasked();
    switch (v.getId()) {
    case R.id.record_audio:
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            textRecordingPress.setVisibility(View.INVISIBLE);
            rect = new Rect(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
            fechaAudioMillis = Calendar.getInstance().getTimeInMillis();
            File directory = new File(
                    Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpeakOn/Audio/Sent");
            directory.mkdirs();//w w  w  .ja v a  2  s  .c om
            ficheroAudio = Environment.getExternalStorageDirectory().getAbsolutePath() + "/SpeakOn/Audio/Sent/"
                    + fechaAudioMillis + ".mp4";
            mediaRecorder = new MediaRecorder();
            mediaRecorder.setOutputFile(ficheroAudio);
            mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
            mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            try {
                mediaRecorder.prepare();
            } catch (IOException e) {
            }
            mediaRecorder.start();
            handler.post(new Runnable() {
                @Override
                public void run() {
                    finalTime += 1000;
                    int seconds = (int) (finalTime / 1000) % 60;
                    int minutes = (int) ((finalTime / (1000 * 60)) % 60);
                    int hours = (int) ((finalTime / (1000 * 60 * 60)) % 24);
                    timeFinal = String.format("%02d", minutes) + ":" + String.format("%02d", seconds);
                    timerAudio.setText(timeFinal);
                    if (!saveAudio) {
                        textRecording.setText(timeFinal);
                    }
                    handler.postDelayed(this, 1000);
                }
            });
            scaleAnimX = ObjectAnimator.ofFloat(recordAudioButton, "scaleX", 1, 1.2f);
            scaleAnimX.setDuration(800);
            scaleAnimX.setRepeatCount(ValueAnimator.INFINITE);
            scaleAnimX.setRepeatMode(ValueAnimator.REVERSE);
            scaleAnimY = ObjectAnimator.ofFloat(recordAudioButton, "scaleY", 1, 1.2f);
            scaleAnimY.setDuration(800);
            scaleAnimY.setRepeatCount(ValueAnimator.INFINITE);
            scaleAnimY.setRepeatMode(ValueAnimator.REVERSE);
            scaleAnimY.start();
            scaleAnimX.start();

            scaleRedBackgroundX = ObjectAnimator.ofFloat(audioRecordBackground, "scaleX", 1, 100f);
            scaleRedBackgroundX.setDuration(200);
            scaleRedBackgroundY = ObjectAnimator.ofFloat(audioRecordBackground, "scaleY", 1, 100f);
            scaleRedBackgroundY.setDuration(200);
            saveAudio = true;
            break;

        case MotionEvent.ACTION_MOVE:
            if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop() + (int) event.getY())) {
                recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_white);
                timerAudio.setVisibility(View.GONE);
                textRecording.setTextColor(getResources().getColor(R.color.speak_all_red));
                if (!animatorBackground) {
                    textRecording.setText(timeFinal);
                    if (scaleRedBackgroundX.isRunning())
                        scaleRedBackgroundX.end();
                    if (scaleRedBackgroundY.isRunning())
                        scaleRedBackgroundY.end();
                    scaleRedBackgroundY.start();
                    scaleRedBackgroundX.start();
                    animatorBackground = true;
                }
                saveAudio = false;
            } else {
                recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_red);
                textRecording.setTextColor(getResources().getColor(R.color.speak_all_white));
                textRecording.setText(getString(R.string.audio_record));
                timerAudio.setVisibility(View.VISIBLE);
                if (animatorBackground) {
                    if (scaleRedBackgroundX.isRunning())
                        scaleRedBackgroundX.end();
                    if (scaleRedBackgroundY.isRunning())
                        scaleRedBackgroundY.end();
                    ViewHelper.setScaleX(audioRecordBackground, 1);
                    ViewHelper.setScaleY(audioRecordBackground, 1);
                    animatorBackground = false;
                }
                saveAudio = true;
            }
            break;
        case MotionEvent.ACTION_UP:
            textRecordingPress.setVisibility(View.VISIBLE);
            animatorBackground = false;
            if (scaleAnimY.isRunning())
                scaleAnimY.end();
            if (scaleAnimX.isRunning())
                scaleAnimX.end();
            if (scaleRedBackgroundX.isRunning())
                scaleRedBackgroundX.end();
            if (scaleRedBackgroundY.isRunning())
                scaleRedBackgroundY.end();
            ViewHelper.setScaleX(audioRecordBackground, 1);
            ViewHelper.setScaleY(audioRecordBackground, 1);
            ViewHelper.setScaleX(recordAudioButton, 1);
            ViewHelper.setScaleY(recordAudioButton, 1);
            handler.removeCallbacksAndMessages(null);
            timerAudio.setText("00:00");
            timerAudio.setVisibility(View.VISIBLE);
            textRecording.setTextColor(getResources().getColor(R.color.speak_all_white));
            textRecording.setText(getString(R.string.audio_record));
            recordAudioButton.setBackgroundResource(R.drawable.rounded_record_audio_red);
            if (finalTime > 1000) {
                mediaRecorder.stop();
                mediaRecorder.release();
            } else {
                saveAudio = false;
                mediaRecorder.release();
            }
            finalTime = 0;
            timeFinal = "";
            if (saveAudio) {
                try {
                    hideKeyBoard();
                    final Message msjAudio = new Message();
                    String id = u.id + contact.idContacto + fechaAudioMillis + Settings.Secure
                            .getString(activity.getContentResolver(), Settings.Secure.ANDROID_ID);
                    msjAudio.mensajeId = id;
                    msjAudio.emisor = u.id;
                    msjAudio.receptor = contact.idContacto;
                    msjAudio.emisorEmail = u.email;
                    msjAudio.receptorEmail = contact.email;
                    msjAudio.emisorLang = u.lang;
                    msjAudio.receptorLang = contact.lang;
                    msjAudio.emitedAt = fechaAudioMillis;
                    msjAudio.tipo = Integer.parseInt(getString(R.string.MSG_TYPE_AUDIO));
                    if (SpeakSocket.mSocket != null)
                        if (SpeakSocket.mSocket.connected()) {
                            msjAudio.status = 1;
                        } else {
                            msjAudio.status = -1;
                        }
                    msjAudio.fileUploaded = false;
                    msjAudio.audioName = ficheroAudio;
                    msjAudio.save();
                    Chats chat = new Select().from(Chats.class).where("idContacto = ?", msjAudio.receptor)
                            .executeSingle();
                    if (chat == null) {
                        Contact contact = new Select().from(Contact.class)
                                .where("id_contact = ?", msjAudio.receptor).executeSingle();
                        Chats newChat = new Chats();
                        newChat.mensajeId = msjAudio.mensajeId;
                        newChat.idContacto = msjAudio.receptor;
                        newChat.isLockedConversation = false;
                        newChat.lastStatus = msjAudio.status;
                        newChat.email = msjAudio.receptorEmail;
                        if (contact != null) {
                            newChat.photo = contact.photo;
                            newChat.fullName = contact.fullName;
                            newChat.lang = contact.lang;
                            newChat.screenName = contact.screenName;
                            newChat.photoload = true;
                            newChat.phone = contact.phone;
                        } else {
                            newChat.photo = null;
                            newChat.photoload = false;
                            newChat.fullName = msjAudio.receptorEmail;
                            newChat.lang = msjAudio.receptorLang;
                            newChat.screenName = msjAudio.receptorEmail;
                            newChat.phone = null;
                        }
                        newChat.emitedAt = msjAudio.emitedAt;
                        newChat.notRead = 0;
                        newChat.lastMessage = "send Audio";
                        newChat.show = true;
                        newChat.save();
                    } else {
                        if (!chat.photoload) {
                            Contact contact = new Select().from(Contact.class)
                                    .where("id_contact = ?", msjAudio.emisor).executeSingle();
                            if (contact != null) {
                                chat.photo = contact.photo;
                                chat.photoload = true;
                            } else {
                                chat.photo = null;
                                chat.photoload = false;
                            }
                        }
                        chat.mensajeId = msjAudio.mensajeId;
                        chat.lastStatus = msjAudio.status;
                        chat.emitedAt = msjAudio.emitedAt;
                        chat.notRead = 0;
                        chat.lastMessage = "send Audio";
                        chat.save();
                    }
                    showNewMessage(msjAudio);

                } catch (Exception e) {
                    // TODO: handle exception
                }
            } else {
                new File(ficheroAudio).delete();
            }
            break;
        }
        break;
    }

    return true;
}