先给大家展示下效果图,如果大家感觉不错,请参考实现代码
效果图如下所示:
代码如下所示:
public class MainActivity extends Activity { VIEw vIEw; public static final int DRAG = 1; public static final int SCALE = 2; int mode = 1; int height = 10,wIDth = 10; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getwindow().requestFeature(Window.FEATURE_NO_Title); setContentVIEw(R.layout.activity_main); vIEw = findVIEwByID(R.ID.vIEw); } float length = 1; // 重写 @OverrIDe public boolean ontouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); // 多指触控 switch (event.getAction() & event.getActionMasked()) { case MotionEvent.ACTION_DOWN: mode = DRAG; break; case MotionEvent.ACTION_POINTER_DOWN: Log.e("TAG","多指移动"); mode = SCALE; // 两个手指开始的长度是多少呢? length = calc(event); break; case MotionEvent.ACTION_UP: length = 1; break; case MotionEvent.ACTION_MOVE: if (mode == DRAG) { // 1. 单个手指 FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( wIDth,height); params.setmargins(x,y,0); vIEw.setLayoutParams(params); } else { // 2. 两个手指 float beilv = calc(event) / length; wIDth = (int) (vIEw.getWIDth() * beilv); height = (int) (vIEw.getHeight() * beilv); Log.e("TAG",beilv + " " + wIDth + " " + height); FrameLayout.LayoutParams params = (LayoutParams) vIEw .getLayoutParams(); params.wIDth = wIDth; params.height = height; vIEw.setLayoutParams(params); } break; } return true; } // 类 Ponint public float calc(MotionEvent event) { float x1 = event.getX(); float y1 = event.getY(); float x2 = event.getX(1); float y2 = event.getY(1); return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }}
xml类
<FrameLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" tools:context="com.example.lesson6_work1.MainActivity" > <VIEw androID:ID="@+ID/vIEw" androID:layout_wIDth="30dp" androID:layout_height="30dp" androID:background="@drawable/oval" /></FrameLayout>
自己在shape中定义的一个圆的oval.xml
<?xml version="1.0" enCoding="utf-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:shape="oval"> <solID androID:color="@androID:color/holo_red_dark"/></shape>
QiuVIEw 类
public class QiuVIEw extends VIEw { Paint paint = new Paint(); PointF point = new PointF(); public QiuVIEw(Context context) { super(context); paint.setcolor(color.RED); paint.setAntiAlias(true); paint.setDither(true); } @OverrIDe protected voID onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawCircle(point.x,point.y,50,paint); } // 触摸事件 @OverrIDe public boolean ontouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_MOVE) { point.set(event.getX(),event.getY()); invalIDate(); } return true; }}
总结
以上所述是小编给大家介绍的AndroID自定义一个图形单点移动缩小的效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对编程小技巧网站的支持!
总结以上是内存溢出为你收集整理的Android自定义一个图形单点移动缩小的效果全部内容,希望文章能够帮你解决Android自定义一个图形单点移动缩小的效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)