如何从画布上单击bitmapdrawable

时间:2014-01-27 04:54:27

标签: android android-animation

我有这个类型BitmapDrawable的图像(球),它移动屏幕上的所有方向,因为我正在animatedview.java中绘制(画布)它很好。我的要求是我只想点击那个图像但不幸的是整个屏幕充当了一个事件。请帮助我,如何点击该图像只是为了让我继续进行更远的开发。我将把我的总代码放在这里

MainActivity

public class MainActivity extends Activity {

    Context context;
    int count=0;
    EditText text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AnimatedView animatedView=(AnimatedView) findViewById(R.id.anim_view);
        animatedView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                EditText text=(EditText) findViewById(R.id.editText1);

                ImageView imageView=(ImageView) findViewById(R.drawable.ball);
                 v.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.animation));


            }
        });

animatedview.java:

public class AnimatedView extends ImageView{

    private Context mContext;
    int x = -1;
    int y = -1;
    private int xVelocity = 10;
    private int yVelocity = 5;
    private Handler h;
    private final int FRAME_RATE = 30;
    BitmapDrawable ball;

    public AnimatedView(Context context, AttributeSet attrs)  {  
        super(context, attrs);  
        mContext = context;  
        h = new Handler();
    } 

    private Runnable r = new Runnable() {
        @Override
        public void run() {
            invalidate(); 
        }
    };

    @Override
    protected void onDraw(Canvas c) {  

        BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball);  
        if (x<0 && y <0) {
            x = this.getWidth()/2;
            y = this.getHeight()/2;
        } else {
            x += xVelocity;
            y += yVelocity;
            if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) {
                xVelocity = xVelocity*-1;
            }
            if ((y > this.getHeight() - ball.getBitmap().getHeight()) || (y < 0)) {
                yVelocity = yVelocity*-1;
            }
        }
        c.drawBitmap(ball.getBitmap(), x, y, null);  

        h.postDelayed(r, FRAME_RATE);    

    }


main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#000000">

<com.example.example.AnimatedView
        android:id="@+id/anim_view"
        android:layout_width="fill_parent"
        android:layout_height="420dp"
        android:onClick="imageClicked" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

你需要实现onTouch(MotionEvent事件),然后你需要检查你当前的触摸坐标是否匹配位图的坐标,如果是,那么你需要实现你想要做的事情。这里有一个如何做的例子那.. ..

@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
 int touchType = event.getAction();
 float x,y;
 switch(touchType){
    case MotionEvent.ACTION_DOWN: 
     //x and y give you your touch coordinates
      x = event.getX();
      y = event.getY();
      /*you just have to chek that your touch coordinates matches
        with current coordinates of bitmap*/
               if(your match is true)
              //do what you want to do.. 
        break;
    case MotionEvent.ACTION_UP :

        break;

    case MotionEvent.ACTION_MOVE:

            break;

    }
    return true;
}