Android跟踪球-手势移动图片-自定义控件(附源码)

由于我不会制作动画图片,所以先放几及其不具备代表性的展示图片.

我以前的思路是通过动态的设置xy坐标通过手势移动来识别,但是我后来试了一下,发现运行效果极差.所以偷闲做了下这个跟踪球控件,其实实现十分简单.只要大家熟悉自定义控件的使用以及手势识别.基本上就ok了.

现在我们看下这个控件的源码TouchMoveView.java

package com.fay.touchmove;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
/**
 * moving by gesture
 * @author Fay
 * @since 2014/5/27
 * {@link [email protected]}
 */
public class TouchMoveView extends View {
    
    private String TAG = "TouchMoveView";
    /**
     * the default bitmap for the TouchMoveView
     */
    private Bitmap defaultBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    
    /**
     * the width of the default bitmap
     */
    private int width = defaultBitmap.getWidth();
    
    /**
     * the height of the default bitmap
     */
    private int height = defaultBitmap.getHeight();
    
    /**
     * the x-Location of the bitmap
     */
    private float xLocation = 0;
    
    /**
     * the y-Location of the bitmap
     */
    private float yLocation = 0;

    public TouchMoveView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public TouchMoveView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public TouchMoveView(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(defaultBitmap, xLocation, yLocation, null);
    }

    @SuppressLint("NewApi")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            float x = event.getX();
            float y = event.getY();
            if (xLocation <= x && x <= xLocation + width && yLocation <= y && y <= yLocation + height) {
                //continue
            } else {
                return false;//end the event
            }
            break;
        case MotionEvent.ACTION_MOVE:
            xLocation = event.getX();
            yLocation =  event.getY();
            break;
        case MotionEvent.ACTION_UP:
            break;
        }
        invalidate();
        return true;
    }

    public Bitmap getDefaultBitmap() {
        return defaultBitmap;
    }
    
    public void setDefaultBitmap(Bitmap defaultBitmap) {
        this.defaultBitmap = defaultBitmap;
        //update the width and the height of the default bitmap
        width = defaultBitmap.getWidth();
        height = defaultBitmap.getHeight();
    }

    public float getxLocation() {
        return xLocation;
    }

    /**
     * set the initialized X-Location
     * @param int xLocation
     */
    public void setxLocation(float xLocation) {
        this.xLocation = xLocation;
    }

    public float getyLocation() {
        return yLocation;
    }
    
    /**
     * set the initialized y-Location
     * @param int yLocation
     */
    public void setyLocation(float yLocation) {
        this.yLocation = yLocation;
    }
    
}

然后看下这个MainActivity.java

package com.fay.touchmove;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.os.Bundle;

@SuppressLint("NewApi")
public class MainActivity extends Activity {
    private String TAG = "MainActivity";
    private TouchMoveView mTouchMoveView = null; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTouchMoveView = (TouchMoveView)findViewById(R.id.imageview);
    //    mTouchMoveView.setDefaultBitmap(BitmapFactory.decodeResource(getResources(),  R.drawable.lock_selected));
    //    mTouchMoveView.setxLocation(100);
    //    mTouchMoveView.setyLocation(100);
    }


}

我们应该能看到在这个Activity中可以很方便的设置移动的图片,以及初始化的x,y坐标.移动过程中通过不断的画图.注意的是,这个控件是全屏的.所以这点的话需要理解.
整个过程及其简单,方便.希望对一些朋友有所帮助.

源码下载:http://files.cnblogs.com/yinweiliang/TouchMove.zip

 

Android跟踪球-手势移动图片-自定义控件(附源码),,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。