加入逻辑线程

12 篇文章 0 订阅

通过线程实现以下效果:
文字会滚动,圆会从0度滚到360度,周而复始

新建LogicView.java,代码如下:

package com.hui.myview.v2;

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

import java.util.Random;

/**
 * Created by Administrator on 2016/4/12.
 */
public class LogicView extends View{

    private Paint paint=new Paint();
    private float rx=0;
    private MyThread thread;
    private RectF rectF=new RectF(0,60,100,160);
    private float sweepAngle=0;

    public LogicView(Context context,AttributeSet attrs){
        super(context,attrs);
    }
    public LogicView(Context context){
        super(context);
    }

    protected void onDraw(Canvas canvas){
        paint.setTextSize(30);
        canvas.drawText("LogicView", rx, 30, paint);

        canvas.drawArc(rectF,0,sweepAngle,true,paint);//绘制圆

        if(thread==null){
            thread=new MyThread();
            thread.start();//调用start方法会自动调用run方法
        }

//        rx=rx+100;
//        canvas.drawText("LogicView",rx,30,paint);
    }

    class MyThread extends Thread{
        Random rand=new Random();

        public void run(){
            while(true){
                rx += 3;

                if(rx>getWidth()){
                    rx=0-paint.measureText("LogicView");
                }
                sweepAngle++;
                if(sweepAngle>360){
                    sweepAngle=0;
                }
                int r= rand.nextInt(256);
                int g= rand.nextInt(256);
                int b= rand.nextInt(256);
                paint.setARGB(255,r,g,b);
                postInvalidate();//实际上是重新调用onDraw方法
                try {
                    Thread.sleep(30);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

此时运行时是MainActivity.java,所以需要在content_main.xml中注册LogicView:

<com.hui.myview.v2.LogicView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

此时运行就能得到目标效果了。


注:

canvas.drawArc(rectF,0,sweepAngle,true,paint);

public void drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint)

oval :指定圆弧的外轮廓矩形区域。
startAngle: 圆弧起始角度,单位为度。
sweepAngle: 圆弧扫过的角度,顺时针方向,单位为度,从右中间开始为零度。
useCenter: 如果为True时,在绘制圆弧时将圆心包括在内,通常用来绘制扇形。
paint: 绘制圆弧的画板属性,如颜色,是否填充等。

thread.start();

1) start:
  用start方法来启动线程,真正实现了多线程运行,这时无需等待run方法体代码执行完毕而直接继续执行下面的代码。通过调用Thread类的start()方法来启动一个线程,这时此线程处于就绪(可运行)状态,并没有运行,一旦得到cpu时间片,就开始执行run()方法,这里方法 run()称为线程体,它包含了要执行的这个线程的内容,Run方法运行结束,此线程随即终止。
  2) run:
  run()方法只是类的一个普通方法而已,如果直接调用Run方法,程序中依然只有主线程这一个线程,其程序执行路径还是只有一条,还是要顺序执行,还是要等待run方法体执行完毕后才可继续执行下面的代码,这样就没有达到写线程的目的。总结:调用start方法方可启动线程,而run方法只是thread的一个普通方法调用,还是在主线程里执行。这两个方法应该都比较熟悉,把需要并行处理的代码放在run()方法中,start()方法启动线程将自动调用 run()方法,这是由jvm的内存机制规定的。并且run()方法必须是public访问权限,返回值类型为void.。

paint.measureText("LogicView");

取得字符串的宽度值。

postInvalidate()实际上是重新调用onDraw方法。

Thread.sleep(30);

线程休眠30ms。

如何自动出现异常捕捉?
alt+/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值