拖动图像工具

时间:2012-02-08 09:50:10

标签: android canvas ontouchevent

我希望在触摸事件中移动图像,因为我的手指移动图像必须用手指的坐标移动。请任何人可以帮助我吗?

1 个答案:

答案 0 :(得分:-1)

package com.hb.skew;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class SkewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        LinearLayout li = new LinearLayout(this);
        li.setGravity(Gravity.CENTER_HORIZONTAL);
        li.setOrientation(LinearLayout.VERTICAL);

        li.addView(new MyView(this),this.getWindowManager().getDefaultDisplay().getWidth(), 100);
    //    li.addView(new MyView(this),this.getWindowManager().getDefaultDisplay().getWidth(), 100);

        setContentView(li);
    }
}
class MyView extends View{

        Paint paint = new Paint();
        public MyView(Context context) {
                super(context);
                setBackgroundColor(Color.BLUE);
                setFocusable(true);
                setFocusableInTouchMode(true);
                paint.setStyle(Style.FILL_AND_STROKE);
                paint.setColor(Color.RED);
                paint.setAntiAlias(true);
        }

        @Override
        protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);

                        RectF rf= new RectF();
                        rf.left=20;
                        rf.top=0;
                        rf.bottom=100;
                        rf.right=100;

                        canvas.skew(+0f, +0.3f);
                        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), null, rf ,paint);
                        //canvas.drawRect(20, 30, 100, 130, paint);    
                        canvas.restore();
                        rf.left+=50;
                        rf.top=30;
                        rf.bottom=130;
                        rf.right+=100;
                        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), null, rf ,paint);

                        canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), x, y ,paint);

        }

        float x,y;
        @Override
        public boolean onTouchEvent(MotionEvent event) {


                x=event.getX();
                y=event.getY();


                invalidate();
                return true;
        }

}

package com.hb.skew;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.graphics.drawable.GradientDrawable.Orientation;
import android.os.Bundle;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class SkewActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        LinearLayout li = new LinearLayout(this);
        li.setGravity(Gravity.CENTER_HORIZONTAL);
        li.setOrientation(LinearLayout.VERTICAL);

        li.addView(new MyView(this),this.getWindowManager().getDefaultDisplay().getWidth(), 100);
    //    li.addView(new MyView(this),this.getWindowManager().getDefaultDisplay().getWidth(), 100);

        setContentView(li);
    }
}
class MyView extends View{

    Paint paint = new Paint();
    public MyView(Context context) {
        super(context);
        setBackgroundColor(Color.BLUE);
        setFocusable(true);
        setFocusableInTouchMode(true);
        paint.setStyle(Style.FILL_AND_STROKE);
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

            RectF rf= new RectF();
            rf.left=20;
            rf.top=0;
            rf.bottom=100;
            rf.right=100;

            canvas.skew(+0f, +0.3f);
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), null, rf ,paint);
            //canvas.drawRect(20, 30, 100, 130, paint); 
            canvas.restore();
            rf.left+=50;
            rf.top=30;
            rf.bottom=130;
            rf.right+=100;
            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), null, rf ,paint);

            canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.icon), x, y ,paint);

    }

    float x,y;
    @Override
    public boolean onTouchEvent(MotionEvent event) {


        x=event.getX();
        y=event.getY();


        invalidate();
        return true;
    }

}