在Imageview的evey touch协会上绘制图像

时间:2015-08-20 06:56:41

标签: android image ontouchlistener android-bitmap

我想在用户触摸android中的现有图像视图的每个点处绘制小圆形图像。并且想要处理每个小图形图像的点击。现在我想在ImageView上绘制circluar图像但是它不工作对我来说。

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF"
    android:orientation="vertical"
    android:weightSum="2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".5"
        android:background="#EEEDEE"
        android:gravity="center"
        android:orientation="horizontal"
        android:padding="20dp"
        android:weightSum="4" >

        <ImageView
            android:id="@+id/img_overview"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/img_specifications"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/img_features"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/img_resources"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_weight="1"
            android:background="@null"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1.5" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:background="#FFFFFF"
            android:orientation="vertical"
            android:padding="20dp" >

            <Textview
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Siss "
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:textColor="#CE561B"
                android:textStyle="bold|italic" />

            <Textview
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:text="dummy long text"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="@color/black_color" />

             <FrameLayout
            android:id="@+id/ll_img_bigview_container"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <com.example.htmlcheck.CustomImageView
                android:id="@+id/img_big_imageview"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@drawable/sj"/>
        </FrameLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

sj图像的尺寸为900 X 600像素。

我的CustomImageView类:

&#13;
&#13;
package com.example.htmlcheck;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;

public class CustomImageView extends ImageView {

	private ArrayList<Point> mTouches;
	private Bitmap mMarker;

	// Java constructor
	public CustomImageView(Context context) {
		super(context);
		init();
	}

	// XML constructor
	public CustomImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		mTouches = new ArrayList<Point>();
		mMarker = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.cross_small);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// Capture a reference to each touch for drawing
		if (event.getAction() == MotionEvent.ACTION_DOWN) {
			mTouches.add(new Point((int) event.getX(), (int) event.getY()));
			return true;
		}

		return super.onTouchEvent(event);
	}

	@Override
	protected void onDraw(Canvas c) {
		// Let the image be drawn first
		super.onDraw(c);

		// Draw your custom points here
		Paint paint = new Paint();
		for (Point p : mTouches) {
			c.drawBitmap(mMarker, p.x, p.y, paint);
		}
	}

}
&#13;
&#13;
&#13;

我的活动:

&#13;
&#13;
public class MainActivity extends Activity implements OnClickListener {

	private CustomImageView mImgBigImageview;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mImgBigImageview = (CustomImageView) findViewById(R.id.img_big_imageview);
		
        mImgBigImageview.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				if (v.onTouchEvent(event)) {
					
				}
				return true;
			}
		});
	}
&#13;
&#13;
&#13;

任何帮助将不胜感激。 感谢。

1 个答案:

答案 0 :(得分:1)

在这里: -

mImgBigImageview.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				CustomImageView mcustomImagview = (CustomImageView) v;
				mcustomImagview.invalidate();
				if (v.onTouchEvent(event)) {
					// Do something with event.getX(), event.getY()
				}
				return true;
			}
		});