android 小球效果,Android开发实现跟随手指的小球效果示例
本文實例講述了android開發實現跟隨手指的小球效果。分享給大家供大家參考,具體如下:
配置drawview類用于繪制小球
public class drawview extends view {
public float currentx = 40;
public float currenty = 50;
//定義并創建畫筆
paint p = new paint();
public drawview(context context)
{
super(context);
}
public drawview(context context , attributeset set)
{
super(context,set);
}
@override
public void ondrawforeground(canvas canvas) {
super.ondrawforeground(canvas);
//設置畫筆顏色
p.setcolor(color.red);
//繪制一個小球
canvas.drawcircle(currentx , currenty , 30 , p);
}
//為組建的觸碰實踐重寫處理方法
@override
public boolean ontouchevent(motionevent event) {
//修改currentx,currenty的兩個屬性
currentx = event.getx();
currenty = event.gety();
//通知當前組建重繪自己
invalidate();
//放回true表明該處理方法已經處理該事件
return true;
}
}
mainactivity
public class mainactivity extends appcompatactivity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
//獲取linearlayout布局容器
linearlayout root = (linearlayout) findviewbyid(r.id.root);
//創建drawview組件
final drawview draw = new drawview(this);
//設定自定義組件的最小寬度、高度
draw.setminimumwidth(300);
draw.setminimumheight(500);
root.addview(draw);
}
}
xml文件
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="match_parent" />
**注:**由上面布局,已經添加了自定義組件,因此activity代碼可簡化為:
public class mainactivity extends appcompatactivity {
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_main);
}
}
示例:
希望本文所述對大家android程序設計有所幫助。
總結
以上是生活随笔為你收集整理的android 小球效果,Android开发实现跟随手指的小球效果示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 比iPhone更具创意 魅族M8屏幕解锁
- 下一篇: android bitmap 饱和度 d