帆布运动圈抽签 [英] Canvas Motion draw circle

查看:129
本文介绍了帆布运动圈抽签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要画一个圆按我的手指移动,所以我有写这篇code

I need to draw a circle as per my finger move so i have write this code

package com.sport;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.BlurMaskFilter.Blur;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class sport extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TouchView(this));

    }

    class TouchView extends View {
        Bitmap bgr;
        Bitmap overlayDefault;
        Bitmap overlay;
        Paint pTouch;

        int X;
        int Y;
        Canvas c2;

        public TouchView(Context context) {
            super(context);

            bgr = BitmapFactory.decodeResource(getResources(), R.drawable.new1);
            overlayDefault = BitmapFactory.decodeResource(getResources(),
                    R.drawable.original);
            overlay = BitmapFactory.decodeResource(getResources(),
                    R.drawable.new1).copy(Config.ARGB_8888, true);
            c2 = new Canvas(overlay);

            pTouch = new Paint(Paint.DEV_KERN_TEXT_FLAG);
            pTouch.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            pTouch.setColor(Color.TRANSPARENT);
            // pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int ht = displaymetrics.heightPixels;
            int wt = displaymetrics.widthPixels;

            X = wt / 2;
            Y = 0;

        }

        @Override
        public boolean onTouchEvent(MotionEvent ev) {

            switch (ev.getAction()) {

            case MotionEvent.ACTION_DOWN: {

                X = (int) ev.getX();
                Y = (int) ev.getY();
                //invalidate();

                break;
            }

            case MotionEvent.ACTION_MOVE: {

                X = (int) ev.getX();
                Y = (int) ev.getY();
                /* Toast.makeText(getBaseContext(),"x is::"+ X,
                          Toast.LENGTH_LONG).show();
                          Toast.makeText(getBaseContext(),"Y is::"+ Y,
                         Toast.LENGTH_LONG).show();*/
                          //c2.drawCircle(X, Y, 70, pTouch);

                /*System.out.println("x is"+X);
                System.out.println("Y is"+Y);*/
                invalidate();
                break;

            }

            case MotionEvent.ACTION_UP:
                X = (int) ev.getX();
                Y = (int) ev.getY();
                /*
                 * Toast.makeText(getBaseContext(),"x is::"+ X,
                 * Toast.LENGTH_LONG).show();
                 * Toast.makeText(getBaseContext(),"Y is::"+ Y,
                 * Toast.LENGTH_LONG).show();
                 */
                //invalidate();
                break;
            case MotionEvent.ACTION_POINTER_ID_MASK :
                /*X = (int) ev.getX();
                Y = (int) ev.getY();



                invalidate();*/
                break;
            }

            return true;
        }

        @Override
        public void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            DisplayMetrics displaymetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
            int ht = displaymetrics.heightPixels;
            int wt = displaymetrics.widthPixels;
            int heightbitmap = bgr.getHeight();
            int widthbitmap = bgr.getWidth();
            int minus = wt - widthbitmap;

            // draw background
            canvas.drawBitmap(bgr, minus / 2, 0, null);
            // copy the default overlay into temporary overlay and punch a hole
            // in it
            c2.drawBitmap(overlayDefault, 360, 0, null); // exclude this line to
                                                            // show all as you
            Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.diff_6);                                              // draw

            //c2.drawBitmap(_scratch, 10, 10, pTouch);
            if(X >= 470 & X <= 522)
            {
                if(Y >= 255 & Y<= 330)
                {   
                //c2.drawRect(X, Y, 5, 5, pTouch);
                    double outer_radius = 0.5 * Math.sqrt((_scratch.getWidth()) * (_scratch.getWidth())+ (_scratch.getHeight()) * (_scratch.getHeight()));  
                 float f = (float) outer_radius;


                    c2.drawCircle(X, Y, f, pTouch);
                }

            }   


            // draw the overlay over the background
            canvas.drawBitmap(overlay, minus/2, 0, null);

        }

    }

}

问题:
当我在屏幕上触摸圈都留在我的手指像这样的右侧。
但是我需要它我手指的中心,请在此帮助

Problem:: when i touch on screen circle are stay at right side of my finger Like this. but i need it center of my finger please help on this

推荐答案

的问题是,因为在创建'覆盖'相对于x和y cordinates从触摸事件,并在后面的偏移(减去复制此到背景/ 2)。假设你的背景图片比屏幕分辨率圈总是会右移,因此将看圆是在你的手指的右侧小。

The problem is because you create 'overlay' with respect to the x and y cordinates from the touch event and later copying this onto the background with an offset (minus/2). Assuming your background image is smaller than the screen resolution the circle will always shift right and hence will look the circle is at the right of your finger.

如果您使用具有相同的分辨率的屏幕尺寸的背景图片,你会得到正确绘制的圆,其在触摸点中心。

If you use a background image which has same resolution as the screen dimensions , you will get properly drawn circle, with its centre at the point of touch.

这篇关于帆布运动圈抽签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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