发布于 2017-10-06 07:04:03 | 93 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

Android移动端操作系统

Android是一种基于Linux的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由Google公司和开放手机联盟领导及开发。尚未有统一中文名称,中国大陆地区较多人使用“安卓”或“安致”。


这篇文章主要为大家详细介绍了Android自定义组件跟随自己手指主动画圆,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Android实现跟随手指画圆的具体代码,供大家参考,具体内容如下

首先自己定义一个View子类:


package com.example.androidtest0.myView;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

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 attrs) {
 super(context, attrs);
 }
 
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 //设置画笔的颜色
 p.setColor(Color.RED);
 //绘制一个小球
 canvas.drawCircle(currentX, currentY, 15, p);
 }
 
 /**
 * 为该组件的触碰事件重写事件处理方法
 */
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 //改动currentX、currentY两个属性
 currentX = event.getX();
 currentY = event.getY();
 //通知当前组件重绘自己
 invalidate();
 return true;
 }
 
  
 

}

主界面XML:

custom_layout.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/root"
  android:orientation="vertical" >

</LinearLayout>

主activity:


package com.example.androidtest0;

import com.example.androidtest0.myView.DrawView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class CustomView extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.custom_layout);
 //获取布局文件里LinearLayout容器
 LinearLayout root = (LinearLayout)findViewById(R.id.root);
 //创建DrawView组件
 final DrawView drawView = new DrawView(this);
 //设置自己定义组件的最小宽度、高度
 drawView.setMinimumWidth(10);
 drawView.setMinimumHeight(10);
 root.addView(drawView);
 }
}


效果:


除此之外:

还能够用XML的方式:也是首先建一个View的子类。和上面一样。

然后主界面XML例如以下:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/root"
  android:orientation="vertical" >

  <com.example.androidtest0.myView.DrawView 
    android:layout_width="match_parent" android:layout_height="match_parent"
    />
</LinearLayout>

主activity文件例如以下:


package com.example.androidtest0;

import com.example.androidtest0.myView.DrawView;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class CustomView extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.custom_layout);
 }
}


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持PHPERZ。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务