突出显示/选择位图中的区域

发布于 2024-11-02 02:38:09 字数 6412 浏览 0 评论 0原文

我试图能够在触摸上选择/突出显示特定的网格位置。 基本上,我有一个图像,在图像的顶部有一个画布/位图,我希望能够单击图像的一个点,并使该框突出显示(第一步是突出显示网格,我将稍后对此执行其他算法)..

这是我现在拥有的代码..我能够获取 xy 坐标,但我不知道如何将 xy 坐标与网格位置相关联。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;


public class ImageWithGridView extends View {
    private int rows = 12;
    private int columns = 12;
    private float width;
    private float height;
    public static final String TAG="ImageWithGridView";
    private static final String SELX = "selX";
    private static final String SELY = "selY";
    private static final String VIEW_STATE = "viewState";
    //private static final int ID = 92;
    private int selX;   //X index of selection
    private int selY;   //Y index of selection
    private final Bitmap bitmap;
    public ImageWithGridView(Context context) {
        super(context);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
        //use your own image file name, instead of mobot_spring
        }

    public ImageWithGridView(Context context, AttributeSet attrs){
        super(context, attrs);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
        //TODO Auto-generated constructor sub
    }
    public ImageWithGridView(Context context, AttributeSet attrs, int defStyle) {
        super (context, attrs, defStyle);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
        //TODO Auto-generated constructor stub
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable p = super.onSaveInstanceState();
        Log.d(TAG, "onSaveInstanceState");
        Bundle bundle = new Bundle();
        bundle.putInt(SELX, selY);
        bundle.putInt(SELY, selY);
        bundle.putParcelable(VIEW_STATE, p);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        Log.d(TAG, "onRestoreInstanceState");
        Bundle bundle = (Bundle) state;
        selX = bundle.getInt(SELX);
        selY = bundle.getInt(SELY);
        super.onRestoreInstanceState(bundle.getParcelable(VIEW_STATE));
        return;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        //adjust the ratio for width and height here:
        this.setMeasuredDimension(parentWidth, parentHeight*2/3);
    }

    @Override
    protected void onSizeChanged(int newWidth, int newHeight, int oldw, int oldh) {
        width = newWidth / (float) columns;
        height = newHeight / (float)rows;
        Log.d(TAG, "~~~~~~~~~~~onSizeChanged: width " + width + ", height " + height);
        super.onSizeChanged(newWidth, newWidth, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        RectF dst = new RectF(width, height, (columns-1) * width, (rows - 1) * height);
        //Draw the background...
        Paint background = new Paint();
        background.setColor(getResources().getColor(R.color.background));
        Log.d(TAG, "~~~~~~~~~10   ");
        canvas.drawRect(0, 0, getWidth(), getHeight(), background);
        Log.d(TAG, "~~~~~~~~~20   ");
        canvas.drawBitmap(bitmap, null, dst, null);
        Log.d(TAG, "~~~~~~~~~30   ");
        //Define colors for the grid lines
        Paint dark = new Paint();
        dark.setColor(getResources().getColor(R.color.dark));
        Paint hilite = new Paint();
        hilite.setColor(getResources().getColor(R.color.hilite));
        Paint light = new Paint();
        light.setColor(getResources().getColor(R.color.light));
        //Draw the minor grid lines

        for (int i = 0; i < rows; i++) {
            canvas.drawLine(0, i * height-1, getWidth(), i * height-1, dark);
            canvas.drawLine(0, i * height, getWidth(), i * height, light);
            canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite);
        }
        for (int i=0; i<columns; i++) {
            canvas.drawLine(i * width-1, 0, i* width-1, getHeight(), dark);
            canvas.drawLine(i * width, 0, i * width, getHeight(), light);
            canvas.drawLine(i * width + 1, 0, i* width + 1, getHeight(), hilite);
        }
        }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN){
            return super.onTouchEvent(event);
        }
        selX = (int) event.getX();
        selY = (int) event.getY();
        Log.d(TAG, "onTouchEvent: x " + selX + ", y " + selY);
        return true;
        }
        public int getClickedX() {return selX;}
        public int getClickedY() {return selY;}
    }

这是我的另一门课,

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class CustomizeImageViewActivity extends Activity{

    /** Called when the activity is first created. */
        private Button  btnShow;
        private TextView label;
        private ImageWithGridView imgView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //setContentView(R.layout.relative_layout);
        //setContentView(R.layout.table_layout);
        btnShow = (Button) findViewById(R.id.btnShow);
        label = (TextView) findViewById(R.id.label);
        imgView = (ImageWithGridView)findViewById(R.id.imageView1);

        btnShow.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        label.setText("("+imgView.getClickedX() + "," + imgView.getClickedY() + ")");
                    }
                });
    }
}

请告知我应该查看的任何指南/教程。我尝试做一些研究,但我无法找到任何相关内容。

I am trying to be able to select/highlight a specific grid position onTouch.
Basically, I have an image, with a canvas/bitmap over layering the top of the image, I want to be able to click on a point of the image, and have that box become highlighted(first step is highlighting the grid, I will perform other algorithms on this later on)..

Here is the code for what I have now.. I am able to grab the x y coordinates, but I do not know how to associate the x y coordinates with the grid position.

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.MeasureSpec;


public class ImageWithGridView extends View {
    private int rows = 12;
    private int columns = 12;
    private float width;
    private float height;
    public static final String TAG="ImageWithGridView";
    private static final String SELX = "selX";
    private static final String SELY = "selY";
    private static final String VIEW_STATE = "viewState";
    //private static final int ID = 92;
    private int selX;   //X index of selection
    private int selY;   //Y index of selection
    private final Bitmap bitmap;
    public ImageWithGridView(Context context) {
        super(context);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
        //use your own image file name, instead of mobot_spring
        }

    public ImageWithGridView(Context context, AttributeSet attrs){
        super(context, attrs);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
        //TODO Auto-generated constructor sub
    }
    public ImageWithGridView(Context context, AttributeSet attrs, int defStyle) {
        super (context, attrs, defStyle);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample);
        //TODO Auto-generated constructor stub
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable p = super.onSaveInstanceState();
        Log.d(TAG, "onSaveInstanceState");
        Bundle bundle = new Bundle();
        bundle.putInt(SELX, selY);
        bundle.putInt(SELY, selY);
        bundle.putParcelable(VIEW_STATE, p);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        Log.d(TAG, "onRestoreInstanceState");
        Bundle bundle = (Bundle) state;
        selX = bundle.getInt(SELX);
        selY = bundle.getInt(SELY);
        super.onRestoreInstanceState(bundle.getParcelable(VIEW_STATE));
        return;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
        int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
        //adjust the ratio for width and height here:
        this.setMeasuredDimension(parentWidth, parentHeight*2/3);
    }

    @Override
    protected void onSizeChanged(int newWidth, int newHeight, int oldw, int oldh) {
        width = newWidth / (float) columns;
        height = newHeight / (float)rows;
        Log.d(TAG, "~~~~~~~~~~~onSizeChanged: width " + width + ", height " + height);
        super.onSizeChanged(newWidth, newWidth, oldw, oldh);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        RectF dst = new RectF(width, height, (columns-1) * width, (rows - 1) * height);
        //Draw the background...
        Paint background = new Paint();
        background.setColor(getResources().getColor(R.color.background));
        Log.d(TAG, "~~~~~~~~~10   ");
        canvas.drawRect(0, 0, getWidth(), getHeight(), background);
        Log.d(TAG, "~~~~~~~~~20   ");
        canvas.drawBitmap(bitmap, null, dst, null);
        Log.d(TAG, "~~~~~~~~~30   ");
        //Define colors for the grid lines
        Paint dark = new Paint();
        dark.setColor(getResources().getColor(R.color.dark));
        Paint hilite = new Paint();
        hilite.setColor(getResources().getColor(R.color.hilite));
        Paint light = new Paint();
        light.setColor(getResources().getColor(R.color.light));
        //Draw the minor grid lines

        for (int i = 0; i < rows; i++) {
            canvas.drawLine(0, i * height-1, getWidth(), i * height-1, dark);
            canvas.drawLine(0, i * height, getWidth(), i * height, light);
            canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, hilite);
        }
        for (int i=0; i<columns; i++) {
            canvas.drawLine(i * width-1, 0, i* width-1, getHeight(), dark);
            canvas.drawLine(i * width, 0, i * width, getHeight(), light);
            canvas.drawLine(i * width + 1, 0, i* width + 1, getHeight(), hilite);
        }
        }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN){
            return super.onTouchEvent(event);
        }
        selX = (int) event.getX();
        selY = (int) event.getY();
        Log.d(TAG, "onTouchEvent: x " + selX + ", y " + selY);
        return true;
        }
        public int getClickedX() {return selX;}
        public int getClickedY() {return selY;}
    }

This is my other class

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class CustomizeImageViewActivity extends Activity{

    /** Called when the activity is first created. */
        private Button  btnShow;
        private TextView label;
        private ImageWithGridView imgView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //setContentView(R.layout.relative_layout);
        //setContentView(R.layout.table_layout);
        btnShow = (Button) findViewById(R.id.btnShow);
        label = (TextView) findViewById(R.id.label);
        imgView = (ImageWithGridView)findViewById(R.id.imageView1);

        btnShow.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View arg0) {
                        label.setText("("+imgView.getClickedX() + "," + imgView.getClickedY() + ")");
                    }
                });
    }
}

Please advise with any guides/tutorials I should look at.. I have tried doing some research but I have not been able to find anything on this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文