android - 在 Imageview 的每个触摸坐标上绘制图像

标签 android image ontouchlistener android-bitmap

我想在用户触摸 android 中现有 ImageView 的每个点绘制小圆形图像。并且想处理每个小绘图图像的点击。现在我正在尝试在 ImageView 上绘制圆形图像,但它不起作用对我来说。

这是我的代码:

<?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 类:

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);
		}
	}

}

我的 Activity :

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;
			}
		});
	}

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

最佳答案

给你:-

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;
			}
		});

关于android - 在 Imageview 的每个触摸坐标上绘制图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32111477/

相关文章:

android - Android 2.3 中的 Navigation Drawer 选择问题

java - 为什么我的拖动方法滞后?

android - 如何在用户仍在android中按住时获取按下按钮的时间

android - 960x540 和 854x480 的小部件布局

java - Android QR 位图需要帮助去除边距

image - 通过CSS按比例调整图像大小

image - 数学 : Transparent background with PNG

python - 如何使用 Python 读取图像文件?

java - 如何使按钮不可点击但可触摸?

android - 谷歌地图不适用于 Android