Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何在ImageView上绘制大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
为什么此代码不会绘制用户在图像视图中触摸并将坐标输出到textview的圆圈?

package com.smallbore.smallbore;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.os.bundle;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.TextView;

public class targetenter extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceStatE) {
        super.onCreate(savedInstanceStatE);
        setContentView(R.layout.targetenter);
    }

   public class ImageView1 extends ImageView {
        public int x;
        public int y;
        public ImageView1(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
        }
        public Boolean dispatchtouchevent(MotionEvent event){
            x = (int) event.getX();
            y = (int) event.getY();
            TextView t1 = (TextView)findViewById(R.id.textView2);
            t1.setText("x="+X);
            TextView t2 = (TextView)findViewById(R.id.textView3);
            t2.setText("y="+y);
            invalidate();
            return true;
        }
        @Override
        public void onDraw(Canvas canvas){
            super.onDraw(canvas);
            canvas.drawCircle(x,y,10,null );
    }
}
}

和xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://scheR_46_11845@as.android.com/apk/res/android" android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_height="fill_parent" android:layout_width="fill_parent">
    <TextView android:layout_height="wrap_content" android:text="@String/cposition" android:id="@+id/textView1" android:layout_width="wrap_content"></TextView>
    <com.smallbore.smallbore.targetenter.Imageview1 android:id="@+id/imageView1" android:layout_width="300dip" android:layout_height="300dip" android:BACkground="@drawable/target" android:layout_gravity="center_horizontal"></com.smallbore.smallbore.targetenter.Imageview1>
    <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
    <TextView android:text="TextView" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

解决方法

将onTouchEvent更改为 dispatchTouchEvent.此外,请确保在XML中使用imageView1,而不仅仅是ImageView.

大佬总结

以上是大佬教程为你收集整理的android – 如何在ImageView上绘制全部内容,希望文章能够帮你解决android – 如何在ImageView上绘制所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。