本文主要是介绍自定义控件(1)---初探之不断闪烁的圆环,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
小案例的原理:
1、new Thread(mCustomView).start();
2、public class CustomView extends View implements Runnable
3、
<strong> public void run() {/*确保线程不断执行不断刷新界面*/while (true) {try {/*如果半径小于200则自加否则大于200后重置半径值以实现往复*/if (radiu <= 200) {radiu += 10;//刷新ViewpostInvalidate();} else {radiu = 0;}// 每执行一次暂停40毫秒Thread.sleep(40);} catch (InterruptedException e) {e.printStackTrace();}}}</strong>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#FFFFFF"android:orientation="vertical" ><com.aigestudio.customviewdemo.views.CustomViewandroid:id="@+id/main_cv"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>
MainActivity
package com.aigestudio.customviewdemo.activities;import android.app.Activity;
import android.os.Bundle;import com.aigestudio.customviewdemo.R;
import com.aigestudio.customviewdemo.views.CustomView;/*** 主界面*/
public class MainActivity extends Activity {private CustomView mCustomView;// 我们的自定义View@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 获取控件mCustomView = (CustomView) findViewById(R.id.main_cv);/** 开线程*/new Thread(mCustomView).start();}
}
MeasureUtil
package com.aigestudio.customviewdemo.views;import android.app.Activity;
import android.util.DisplayMetrics;/*** 测绘工具类*/
public final class MeasureUtil {/*** 获取屏幕尺寸* * @param activity* Activity* @return 屏幕尺寸像素值,下标为0的值为宽,下标为1的值为高*/public static int[] getScreenSize(Activity activity) {DisplayMetrics metrics = new DisplayMetrics();activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);return new int[] { metrics.widthPixels, metrics.heightPixels };}
}
package com.aigestudio.customviewdemo.views;import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;/*** 自定义View*/
public class CustomView extends View implements Runnable {private Paint mPaint;// 画笔private Context mContext;// 上下文环境引用private int radiu;// 圆环半径public CustomView(Context context) {this(context, null);}public CustomView(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;// 初始化画笔initPaint();}/*** 初始化画笔*/private void initPaint() {// 实例化画笔并打开抗锯齿mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);/** 设置画笔样式为描边,圆环嘛……当然不能填充不然就么意思了* * 画笔样式分三种:* 1.Paint.Style.STROKE:描边* 2.Paint.Style.FILL_AND_STROKE:描边并填充* 3.Paint.Style.FILL:填充*/mPaint.setStyle(Paint.Style.STROKE);// 设置画笔颜色为浅灰色mPaint.setColor(Color.LTGRAY);/** 设置描边的粗细,单位:像素px* 注意:当setStrokeWidth(0)的时候描边宽度并不为0而是只占一个像素*/mPaint.setStrokeWidth(10);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);// 绘制圆环canvas.drawCircle(MeasureUtil.getScreenSize((Activity) mContext)[0] / 2, MeasureUtil.getScreenSize((Activity) mContext)[1] / 2, radiu, mPaint);}@Overridepublic void run() {/** 确保线程不断执行不断刷新界面*/while (true) {try {/** 如果半径小于200则自加否则大于200后重置半径值以实现往复*/if (radiu <= 200) {radiu += 10;// 刷新ViewpostInvalidate();} else {radiu = 0;}// 每执行一次暂停40毫秒Thread.sleep(40);} catch (InterruptedException e) {e.printStackTrace();}}}
}
这篇关于自定义控件(1)---初探之不断闪烁的圆环的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!