如何让canvas绘制的图表生成onclick事件

时间:2012-12-18 08:56:41

标签: android canvas onclick

我有一个imageView,我根据坐标数组在imageView中绘制图表。每个图都有一个onclick事件。 Activity.java

package com.example.floorexhibitiontest;
import com.floor.DrawView;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class HallActivity extends Activity {@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hall);
    }

    float[][][] points = new float[][][] {
        {
            {213,264},
            {247,232},
            {345,338},
            {310,371}
        },
        {
            {171,305},
            {205,272},
            {302,373},
            {267,406}
        },
        {
            {571,320},
            {606,320},
            {606,428},
            {571,428}
        }
    };

    LinearLayout layout = (LinearLayout)findViewById(R.id.root);
    final DrawView draw = new DrawView(this,points);
    layout.addView(draw);       
}

DrawView.java

package com.floor;
import com.example.floorexhibitiontest.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.graphics.Shader.TileMode;
import android.graphics.Shader;
import android.util.DisplayMetrics;
import android.view.View;

public class DrawView extends View{
    private float[][][] points = null;
    DisplayMetrics metric = new DisplayMetrics();

    public DrawView(Context context,float[][][] p) {
        super(context);
        metric = context.getApplicationContext().getResources().getDisplayMetrics();
        points = p;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        float density=metric.density;  

        for(int i = 0; i < points.length; i++){
            Paint p = new Paint();
            p.setAntiAlias(true);
            p.setColor(Color.BLUE);
            Path path=new Path();
            path.moveTo(points[i][0][0] / density, points[i][0][1] / density);
            path.lineTo(points[i][1][0] / density, points[i][1][1] / density);
            path.lineTo(points[i][2][0] / density, points[i][2][1] / density);
            path.lineTo(points[i][3][0] / density, points[i][3][1] / density);

            path.close();
            p.setStyle(Style.STROKE);
            canvas.drawPath(path, p);
        }
    }
}

目前效果: enter image description here

  1. 如何让每个绘制的矩形产生自己的onclick事件?
  2. 布局文件中有一个ImageView。我的目的是将图形绘画放在ImageView上。但结果是ImageView可以显示。绘制的图形无法显示。如果我躲起来 ImageView,图像可以显示。
  3. 请大家帮忙。谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

尝试这样的smth:

public boolean onTouch(View v, MotionEvent event) {
   if((event.getX(0)>=x_coord) && 
      (event.getY(0)>=y_coord) && 
     ( event.getX(0)<=x_coord + your_rectangle_width) && 
      (event.getY(0)<=y_coord + your_rectangle_heigth))
      {
          //rectangle selected
      }
   return true;
}

或者在onTouch()中使用points [] array

中的实际坐标