将侦听器附加到android中的某些视图部分

时间:2014-04-16 03:36:49

标签: android android-layout listview views

我有一个视图,其中有一个矩形,几条线从矩形边缘出来。我已经为整个视图设置了onClickListener,但是有什么方法可以为视图的各个部分(行)设置onClickListener?

我的问题是我们是否可以将侦听器设置为android中的部分视图?

我的视图类(DrawView.java)

    public class DrawView extends View {
    Paint paint = new Paint();
    Rect r =new Rect(0, 0, 300, 40);



    public DrawView(Context context) {
        this(context, null);            
    }

    public DrawView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);


    }

    @Override
    public void onDraw(Canvas canvas) {

        paint.setColor(Color.GREEN);
        canvas.save();
        canvas.scale(mScaleFactor, 1);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE); 
        canvas.drawRect(r, paint);
        makeLinesinRange(0,40,300,40,300, 10, canvas,100);  // this will make 10 lines from edge of rectangle in downward side 
        requestLayout();
       }






    private void makeLinesinRange(int x1, int y1, int x2, int y2, int size, int total_events, Canvas canvas,int width)
    {
        paint.setStrokeWidth(6);
        paint.setColor(Color.GRAY);

        if(total_events<1)
        {
            return;
        }
        if(total_events==1)
        {
            canvas.drawLine(((x1+x2)/2), y1, (x1+x2)/2, y2+size, paint);

        }
        else if(total_events==2)
        {
            canvas.drawLine(((x1+x2)/3), y1, (x1+x2)/3, y2+size, paint);
            canvas.drawLine((2*(x1+x2)/3), y1, 2*(x1+x2)/3, y2+size, paint);


        }
        else
        {
        paint.setStrokeWidth(6);
        paint.setColor(Color.GRAY);
        float kk=(x2-x1)/(total_events-1);
        for(int i=0;i<total_events;i++)
        {
            canvas.drawLine(x1+i*kk, y1, x1+i*kk, y2+size, paint);
        }
        }
    }


}

我的活动包含了drawview

    public class MainActivity extends Activity {
    DrawView drawView;


    public class DrawView extends View {
        Paint paint = new Paint();
        Rect r = new Rect(0, 0, 300, 40);
        float mScaleFactor = 1.0f;

        int width = 5;
        int height = 300;

        List<HashMap<String, RectF>> lines = new ArrayList<HashMap<String, RectF>>();

        public DrawView(Context context) {
            this(context, null);
        }

        public DrawView(Context context, AttributeSet attrs) {
            this(context, attrs, 0);
        }

        public DrawView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);

        }

        @Override
        public void onDraw(Canvas canvas) {

            paint.setColor(Color.GREEN);
            canvas.save();
            canvas.scale(mScaleFactor, 1);
            paint.setStyle(Paint.Style.FILL);
            paint.setColor(Color.BLUE);
            canvas.drawRect(r, paint);
            makeLinesinRange(10, canvas); // this will
                                            // make 10 lines
                                            // from edge of
                                            // rectangle in
                                            // downward side
            requestLayout();
        }

        private void makeLinesinRange(int total_events, Canvas canvas) {
            paint.setStrokeWidth(6);
            paint.setColor(Color.GRAY);

            int x = 0;
            int y = 40;
            int range = r.width()/total_events;
            range= range + ((range*10)/100);

            for (int i = 0; i < total_events; i++) {
                RectF r = new RectF();
                r.set(x, y, x + width, y + height);
                Paint mPaint = new Paint();
                mPaint.setPathEffect(null);
                canvas.drawRect(r, paint);
                HashMap<String, RectF> item = new HashMap<String, RectF>();
                item.put("" + i, r);
                lines.add(item);
                x+=range;
            }
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_UP) {

                for (int i = 0; i < lines.size(); i++) {
                    RectF r = lines.get(i).get("" + i);

                    if (r.contains(event.getX(), event.getY())) {
                        Toast.makeText(getContext(), "" + (i+1) + " line clicked", Toast.LENGTH_SHORT).show();
                        System.out.println((i+1) + " line clicked");
                        break;
                    }
                }
                return false;
            }
            return true;
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        drawView = new DrawView(this);
        drawView.setBackgroundColor(Color.WHITE);
        setContentView(drawView);




    }
} 

现在我的新代码onDraw方法是
        Rect r = new Rect(0,0,700,40);

    public void onDraw(Canvas canvas) {


        width=sf*700;

        canvas.save();

        makeLinesinRangetwo(10, canvas); // this will


         paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE); 
        canvas.drawRect(r, paint);
        canvas.scale(sf,1,100,200);
        requestLayout();
        canvas.restore();
        }

在这种情况下,只有线条在缩放时展开,但矩形不是。

1 个答案:

答案 0 :(得分:1)

试试希望帮助你。

我已在您的线路上实施点击事件。试试这段代码。

public class DrawView extends View {
    Paint paint = new Paint();
    Rect r = new Rect(0, 0, 300, 40);
    float mScaleFactor = 1.0f;

    int width = 5;
    int height = 300;

    List<HashMap<String, RectF>> lines = new ArrayList<HashMap<String, RectF>>();

    public DrawView(Context context) {
        this(context, null);
    }

    public DrawView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    @Override
    public void onDraw(Canvas canvas) {

        paint.setColor(Color.GREEN);
        canvas.save();
        canvas.scale(mScaleFactor, 1);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE);
        canvas.drawRect(r, paint);
        makeLinesinRange(10, canvas); // this will
                                        // make 10 lines
                                        // from edge of
                                        // rectangle in
                                        // downward side
        requestLayout();
    }

    private void makeLinesinRange(int total_events, Canvas canvas) {
        paint.setStrokeWidth(6);
        paint.setColor(Color.GRAY);

        int x = 0;
        int y = 40;
        int range = r.width()/total_events;
        range= range + ((range*10)/100);

        for (int i = 0; i < total_events; i++) {
            RectF r = new RectF();
            r.set(x, y, x + width, y + height);
            Paint mPaint = new Paint();
            mPaint.setPathEffect(null);
            canvas.drawRect(r, paint);
            HashMap<String, RectF> item = new HashMap<String, RectF>();
            item.put("" + i, r);
            lines.add(item);
            x+=range;
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_UP) {

            for (int i = 0; i < lines.size(); i++) {
                RectF r = lines.get(i).get("" + i);

                if (r.contains(event.getX(), event.getY())) {
                    Toast.makeText(getContext(), "" + (i+1) + " line clicked", Toast.LENGTH_SHORT).show();
                    System.out.println((i+1) + " line clicked");
                    break;
                }
            }
            return false;
        }
        return true;
    }

}