Android跟踪球-手势移动图片-自定义控件(附源码)
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android跟踪球-手势移动图片-自定义控件(附源码),小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3685字,纯文字阅读大概需要6分钟。
内容图文
![Android跟踪球-手势移动图片-自定义控件(附源码)](/upload/InfoBanner/zyjiaocheng/1096/e7a9bada4a104bac92e6f704294257e9.jpg)
由于我不会制作动画图片,所以先放几及其不具备代表性的展示图片.
我以前的思路是通过动态的设置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 1940125001@qq.com} */ 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 */privateint width = defaultBitmap.getWidth(); /** * the height of the default bitmap */privateint height = defaultBitmap.getHeight(); /** * the x-Location of the bitmap */privatefloat xLocation = 0; /** * the y-Location of the bitmap */privatefloat 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 protectedvoid onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(defaultBitmap, xLocation, yLocation, null); } @SuppressLint("NewApi") @Override publicboolean 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 { returnfalse;//end the event } break; case MotionEvent.ACTION_MOVE: xLocation = event.getX(); yLocation = event.getY(); break; case MotionEvent.ACTION_UP: break; } invalidate(); returntrue; } public Bitmap getDefaultBitmap() { return defaultBitmap; } publicvoid setDefaultBitmap(Bitmap defaultBitmap) { this.defaultBitmap = defaultBitmap; //update the width and the height of the default bitmap width = defaultBitmap.getWidth(); height = defaultBitmap.getHeight(); } publicfloat getxLocation() { return xLocation; } /** * set the initialized X-Location * @param int xLocation */publicvoid setxLocation(float xLocation) { this.xLocation = xLocation; } publicfloat getyLocation() { return yLocation; } /** * set the initialized y-Location * @param int yLocation */publicvoid 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") publicclass MainActivity extends Activity { private String TAG = "MainActivity"; private TouchMoveView mTouchMoveView = null; @Override protectedvoid 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
原文:http://www.cnblogs.com/yinweiliang/p/3754711.html
内容总结
以上是互联网集市为您收集整理的Android跟踪球-手势移动图片-自定义控件(附源码)全部内容,希望文章能够帮你解决Android跟踪球-手势移动图片-自定义控件(附源码)所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。