在Imageview的evey touch标准上绘制图像 [英] Draw images on evey touch co-ordinats Of Imageview

查看:156
本文介绍了在Imageview的evey touch标准上绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to draw small circular images at every point where user touches existing Image-view in android.And want to handle the click of every small ploted image.Right now I am trying to draw circluar image on ImageView but its not working for me.

这是我的代码:

<?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像素。

dimension of sj image is 900 X 600 pixels.

我的CustomImageView类:

My CustomImageView Class :

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

}

我的活动:

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

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

Any Help will be appreciated. Thanks.

推荐答案

在这里你去: -

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

这篇关于在Imageview的evey touch标准上绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆