Android 运动的小车
来源:互联网 发布:java框架有必要学吗 编辑:程序博客网 时间:2024/04/30 04:57
先看看第一种效果
第一种效果是背景动,小车不动,看上去是小车在动!
这里需要三张背景图
然后来看看怎么实现
先自定义一个view来使背景移动
附上代码
public class ScrollingView extends View { private float mSpeed; private Bitmap mBitmap; private Paint mPaint; private float mOffset = 0; public ScrollingView(Context context) { super(context); } public ScrollingView(Context context, AttributeSet attrs) { super(context, attrs); //读取自定义的背景和速度 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScrollingView); mSpeed = typedArray.getFloat(R.styleable.ScrollingView_speed, 1.0f); int resourceId = typedArray.getResourceId(R.styleable.ScrollingView_scrollingBackground, 0); //设置位图 mBitmap = BitmapFactory.decodeResource(getResources(), resourceId); //回收typedArray typedArray.recycle(); mPaint = new Paint(); } //确定控件大小 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //已经知道了mode,直接拿大小 int width = MeasureSpec.getSize(widthMeasureSpec); setMeasuredDimension(width, mBitmap.getHeight()); } //画出来 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(mOffset<-mBitmap.getWidth()) { mOffset=0; } int left = (int) mOffset; while (left < getMeasuredWidth()) { canvas.drawBitmap(mBitmap, left, 0, mPaint); left = left + getMeasuredWidth(); } mOffset = mOffset - mSpeed; //调用ondraw invalidate(); }}
布局
<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <!--背景--> <com.sihaiwanlian.cmccnev.widget.ScrollingView android:id="@+id/scrollingView_bg" android:layout_width="match_parent" android:layout_height="wrap_content" yoyo:scrollingBackground="@mipmap/scrolling_background" yoyo:speed="3.0" /> <!--前景--> <com.sihaiwanlian.cmccnev.widget.ScrollingView android:id="@+id/scrollingView_fore" android:layout_width="match_parent" android:layout_height="wrap_content" yoyo:scrollingBackground="@mipmap/scrolling_foreground" yoyo:speed="4.8" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@mipmap/scrolling_car" /> </FrameLayout>
搞定!
再来看看第二种效果
当然图片还是一样
代码就不一样了
public class ScrollingView extends View { private float mSpeed; private Bitmap mBitmap; private Paint mPaint; private float mOffset = 0; public ScrollingView(Context context) { super(context); } public ScrollingView(Context context, AttributeSet attrs) { super(context, attrs); //读取自定义的背景和速度 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScrollingView); mSpeed = typedArray.getFloat(R.styleable.ScrollingView_speed, 1.0f); int resourceId = typedArray.getResourceId(R.styleable.ScrollingView_scrollingBackground, 0); //设置位图 mBitmap = BitmapFactory.decodeResource(getResources(), resourceId); //回收typedArray typedArray.recycle(); mPaint = new Paint(); } //确定控件大小 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //已经知道了mode,直接拿大小 int width = MeasureSpec.getSize(widthMeasureSpec); setMeasuredDimension(width, mBitmap.getHeight()); } //画出来 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (mOffset > getMeasuredWidth()) { mOffset = 0; } int left = (int) mOffset; canvas.drawBitmap(mBitmap, left, 0, mPaint); mOffset = mOffset + mSpeed; invalidate(); }}
布局
<FrameLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@mipmap/scrolling_foreground" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:src="@mipmap/scrolling_background" /> <com.sihaiwanlian.cmccnev.widget.ScrollingView android:id="@+id/scrollingView_fore" android:layout_width="match_parent" android:layout_height="wrap_content" yoyo:scrollingBackground="@mipmap/scrolling_car" yoyo:speed="4.8" /> </FrameLayout>
ok,搞定!
阅读全文
1 0
- Android 运动的小车
- zrobot----------小车的运动控制
- LCD1602显示小车的运动方向
- 【java】java实现运动的小车
- 基于android和arduino 的小车控制
- 手机控制urdf小车模型运动(一)—安装indigo下的串口驱动
- 手机控制urdf小车模型运动(二)-创建简单的机器人模型
- 使用简单的URDF模型文件结合ros小车实现同步运动
- ros 两轮小车运动模型
- 手机控制urdf小车模型运动(三)-手机控制小车运动
- Android运动事件的处理
- Android动画-滚动小车
- 自己的android控制的飞思卡尔小车
- WifiRobot -android 一个wifi控制的四驱小车
- 基于android系统的智能小车控制软件开发
- 基于UDP协议的小车控制android软件
- Android手机控制树莓派制作的四驱小车
- 项目经验:Android嵌入式系统控制的蓝牙小车
- HDU 5833 Zhu and 772002
- 在Linux下安装Android SDK
- YII修改默认控制器SiteController
- Python的6种内建序列操作
- Linux下通过命令行创建和设置一个MySQL用户
- Android 运动的小车
- 重定位
- 6.S5PV210时钟初始化c语言实现代码
- 如何设计一个在线烧录方案?
- Java的代理模式(一)静态代理
- lintcode --不同的二叉查找树
- PAT-L1-002. 打印沙漏
- 深入理解JVM--我的学习笔记
- NYOJ 石子合并(一)经典区间DP