android - 当 ANDROID 中没有更多 Action 移动事件时如何获取 Action?

标签 android motion

我在 MotionEvent 上旋转图像。当我旋转它时,图像也会缩小。我想在移动事件不多时显示警报对话框..

我在这里发布我的代码...

私有(private)ProgressBar distanceBar; 私有(private)按钮回来; 私有(private) ImageView setAccuracy; float 当前度数= 0; float 当前距离; float 总距离 = 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.setOnTouchListener(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);

}

我怎样才能克服这个问题?

谢谢...

最佳答案

在您的案例中使用 MotionEvent.ACTION_UP 代替 MotionEvent.ACTION_CANCEL。

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;

关于android - 当 ANDROID 中没有更多 Action 移动事件时如何获取 Action?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6936742/

相关文章:

c++ - 从二值图像中屏蔽一个 Blob

java - Android Studio 中动态列出按钮(相对布局)

android - 在后退按钮上重置卡片 View 的内容

ios - 检测 iPhone 底部的凹凸

c++ - sdl 45 度运动

iphone - 加速度计相对位置

iphone - 如何将 void(摇动手势)放入 IBAction(按钮)中?

android - 自定义ExpandableListView时默认箭头继续出现

java - 编写小型服务器的最快方法

android - 约束布局与百分比相对布局