0

ユーザーのタッチに応じて画像をアニメーション化する必要があるプロジェクトに取り組んでいますが、問題はすべてのタッチポイントを取得できないため、スムーズなアニメーションが得られないことです。私はAndroidタブレットOS4.0.3に取り組んでいます。

   public class DrawView extends View implements OnTouchListener {
    private static final String TAG = "DrawView";

    List<Points> points = new ArrayList<Points>();
    Paint paint = new Paint();
    Button button = null;
    int movement = 0;

    int[] imageIds = {R.drawable.zero,R.drawable.one,R.drawable.two,R.drawable.three,R.drawable.four,
            R.drawable.five,R.drawable.six,R.drawable.seven,R.drawable.eight,R.drawable.ninie,R.drawable.ten,
            R.drawable.eleven,R.drawable.twelve,R.drawable.thirteen,R.drawable.fourteen,R.drawable.fifteen,
            R.drawable.sixteen,R.drawable.seventeen,R.drawable.eighteen,R.drawable.ninteen,R.drawable.twenty,
            R.drawable.twentyone,R.drawable.twentytwo,R.drawable.twentythree,R.drawable.twentyfour,
            R.drawable.twentyfive,R.drawable.twentysix,R.drawable.twentyseven,R.drawable.twentyeight,
            R.drawable.twentynine,R.drawable.thirty,R.drawable.thirtyone,R.drawable.thirtytwo,
            R.drawable.thirtythree,R.drawable.thirtyfour,R.drawable.thirtyfive,R.drawable.thirtysix,
            R.drawable.thirtyseven,R.drawable.thirtyeight,R.drawable.thirtynine,R.drawable.fortyone,
            R.drawable.fortytwo,R.drawable.fortytwo,R.drawable.fortyfour,R.drawable.fortyfive,
            R.drawable.fortysix,R.drawable.fortyseven,R.drawable.fortyeight,R.drawable.fortynine,
            R.drawable.fifty,R.drawable.fiftyone,R.drawable.fiftyone,R.drawable.fiftythree,R.drawable.fiftyfour,
            R.drawable.fiftyfive,R.drawable.fiftysix,R.drawable.fiftyseven,R.drawable.fiftyeight,
            R.drawable.fiftynine,R.drawable.sixty,R.drawable.sixtyone,R.drawable.sixtytwo,
            R.drawable.sixtythree,R.drawable.sixtyfour,R.drawable.sixtyfive,R.drawable.sixtysix,
            R.drawable.sixtyseven,R.drawable.sixtyeight,R.drawable.sixtynine,R.drawable.seventy,
            R.drawable.seventyone,R.drawable.seventytwo,R.drawable.seventythree,R.drawable.seventyfour,
            R.drawable.seventyfive,R.drawable.seventysix,R.drawable.seventyseven,R.drawable.seventyeight,
            R.drawable.seventynine,R.drawable.eighty,R.drawable.eightyone,R.drawable.eightytwo,R.drawable.thirtythree,
            R.drawable.eightyfour,R.drawable.eightyfive,R.drawable.eightysix,R.drawable.eightyseven,
            R.drawable.seventyeight,R.drawable.eightynine,R.drawable.ninty,R.drawable.nintyone,
            R.drawable.nintytwo,R.drawable.nintythree,R.drawable.nintyfour,R.drawable.nintyfive,
            R.drawable.nintysix,R.drawable.nintyseven,R.drawable.nintyeight,R.drawable.nintynine,
            R.drawable.hundred
    };
    int touchPoint=0,currentImage=0;
    float previousX = 0, starting = 0, difference = 0, remaining=0;
    RelativeLayout relativeLayout;
    Boolean update = false;
    int changeImageFrequency = 4;

    ImageView image;

    public DrawView(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setOnTouchListener(this);

    }

    @SuppressLint("DrawAllocation")
    @TargetApi(11)
    @Override
    public void onDraw(Canvas canvas) {
        Log.i("testing", "i am in draw");

        if (movement==0) {
            Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
            canvas.drawBitmap(bimap, 0, 0,null);
            invalidate();
        }
        else if (movement==1) {
            if (currentImage+changeImageFrequency<100) {
                currentImage+=changeImageFrequency;
                Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
                canvas.drawBitmap(bimap, 0, 0,null);
                invalidate();
            }
            else {
                Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
                canvas.drawBitmap(bimap, 0, 0,null);
                currentImage=0;
            }
            movement=0;
            invalidate();
        }
        else if (movement==2) {

            if (currentImage-changeImageFrequency>=0) {
                currentImage-=changeImageFrequency;
                Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
                canvas.drawBitmap(bimap, 0, 0,null);
                invalidate();
            }
            else {
                Bitmap bimap = BitmapFactory.decodeResource(getResources(), imageIds[currentImage]);
                canvas.drawBitmap(bimap, 0, 0,null);
                currentImage=99;
            }
            movement=0;
            invalidate();
        }
    }

    public boolean onTouch(View view, MotionEvent event) {
        // TODO Auto-generated method stub
        int action = event.getAction();
        switch(action) {
        case MotionEvent.ACTION_DOWN:
            starting = event.getX();
            invalidate();
            break;
        case MotionEvent.ACTION_MOVE:   
            difference =  event.getX()-starting;
            if (difference>0)
                movement = 1;
            else if (difference<0)
                movement = 2;
            invalidate();
            break;
        case MotionEvent.ACTION_UP:
            invalidate();
            break;
        }
        return true;
    }
4

1 に答える 1

0

画像の解像度が平均的な場合BitmapFactory.decodeResource、メインスレッドで画像をロードすると()、UIがブロックされ、ロードが実行されるまで一部のタッチイベントがスキップされます。別のスレッドで描画を実行するため、SurfaceView代わりにを使用することをお勧めします。SurfaceViewの使用の詳細については、Viewこのリンクを確認してください。

于 2012-10-03T13:52:35.507 に答える