1

我有一个 imageView,我根据坐标数组在 imageView 中绘制图表。每个图表都有一个 onclick 事件。活动.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);       
}

绘图视图.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);
        }
    }
}

目前效果: 在此处输入图像描述

  1. 如何让每个绘制的矩形产生自己的onclick事件?
  2. 布局文件中有一个 ImageView。我的目的是将图形绘制在 ImageView 上。但结果是ImageView可以显示。绘制的图形无法显示。如果我隐藏 ImageView,图像可以显示。

求大家帮忙。谢谢指教。

4

1 回答 1

0

像这样尝试:

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[] 数组的实际坐标

于 2012-12-18T09:08:05.280 回答