开发者

How to get Action when motion move event is not more in ANDROID?

开发者 https://www.devze.com 2023-03-24 22:55 出处:网络
I am Rotating image on MotionEvent. and also image is getting shrink when i rotate it. i want to show Alert Dialog when move event is not more..

I am Rotating image on MotionEvent. and also image is getting shrink when i rotate it. i want to show Alert Dialog when move event is not more..

here i post my code...

private ProgressBar distanceBar; private Button back; private ImageView setAccuracy; float currentDegrees = 0; float currentDistance; float totalDistance = 1000; float totalDegree = 360;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.errandboy_destination);

    back = (Button) findViewById(R.id.back);
    distanceBar = (ProgressBar) findViewById(R.id.errand_progress);
    distanceBar.setMax(1000);
    setAccuracy = (ImageView) findViewById(R.id.errandAnim);
    OnTouchListener backListener = new OnTouchListener() {
        public boolean  onTouch  (View  v, MotionEvent  event) {
            if (event.getAction()==MotionEvent.ACTION_UP){
                finish();
            }
            return false;
        }
    };
    back.setOnTouchListen开发者_如何学运维er(backListener);
    setAccuracy.setOnTouchListener(onTableTouched);
}
public android.view.View.OnTouchListener onTableTouched = new android.view.View.OnTouchListener() {
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
           case MotionEvent.ACTION_MOVE:
                if (currentDegrees > 360) {
                    currentDegrees = 0;
                }
                currentDegrees = currentDegrees + 1;
                updateRotation(currentDegrees);
                System.out.println("Current Degree is:--->" + currentDegrees);
                currentDistance = (currentDegrees * totalDistance)
                        / totalDegree;
                System.out
                        .println("Current Distance is:--->" + currentDistance);
                int progress = (int) currentDistance;
                distanceBar.setProgress(progress);
                break;

           case MotionEvent.ACTION_CANCEL:
            AlertDialog.Builder choice = new Builder(getParent());
            choice.setTitle("Error");
            choice.setMessage("message");
            choice.setPositiveButton("Ok",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            return;
                        }
                    });
            choice.create().show();
            break;
        }

        return true;
    }
};
private void updateRotation(double rot) {
    float newRot = new Float(rot);
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.anim1);
    int width = bitmapOrg.getWidth();
    int height = bitmapOrg.getHeight();
    int newWidth = 200;
    int newHeight = 200;
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    int centerX = width/2;
    int centerY = height/2;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);
    matrix.postRotate(newRot);
    matrix.postTranslate(centerX, centerY);
    matrix.preTranslate(-centerX, -centerY);
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width, height, matrix, true);
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
    setAccuracy.setImageDrawable(bmd);

}

}

how can i overcome this problem?

Thanks...


Use the MotionEvent.ACTION_UP in place of MotionEvent.ACTION_CANCEL in your case.

case MotionEvent.ACTION_UP:
        AlertDialog.Builder choice = new Builder(getParent());
        choice.setTitle("Error");
        choice.setMessage("message");
        choice.setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        return;
                    }
                });
        choice.create().show();
        break;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号