android自定义View探索4(文字跑马灯)

前言:

人类精神必须置于技术之上。--阿尔伯特▪爱因斯坦


今天我们用自定义View实现文字跑马灯效果


先来看看效果图



一.属性介绍


我们设置了三个属性分别是尺寸、颜色、文字内容

属性名简介属性取值类型示例
scrollTextSize文字尺寸dimension14dp
scrollTextColor文字颜色color@color/colorAccent
scrollText文字内容string这是一个跑马灯



二.设计属性


第一步 values目录下新建attrs.xml


第二部 设计属性


    <!--文字跑马灯-->
    <declare-styleable name="ViewTwo">
        <!--文字尺寸-->
        <attr name="scrollTextSize" format="dimension"/>
        <!--文字颜色-->
        <attr name="scrollTextColor" format="color"/>
        <!--文字内容-->
        <attr name="scrollText" format="string"/>
    </declare-styleable>



三.自定义View

ViewTow.class
 
 
package myview.csdn.com.view.two;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import myview.csdn.com.R;

/**
 * 实现跑马灯
 *
 * Created by Administrator on 2017/6/19.
 */

public class ViewTwo  extends View{

    private Paint paint=new Paint();

    private int scrollTextSize=30;//文字尺寸
    private int scrollTextColor=getResources().getColor(R.color.colorBlack);//文字颜色
    private String scrollText="测试文字";
    private int rx=0;//绘制文字x坐标
    private MyThread thread;

    public ViewTwo(Context context) {
        super(context);
    }

    public ViewTwo(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray ta=context.obtainStyledAttributes(attrs,R.styleable.ViewTwo);
        scrollTextSize=ta.getDimensionPixelSize(R.styleable.ViewTwo_scrollTextSize,12);
        scrollTextColor=ta.getInt(R.styleable.ViewTwo_scrollTextColor,R.color.colorBlack);
        scrollText=ta.getString(R.styleable.ViewTwo_scrollText);
        ta.recycle();//释放
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setTextSize(scrollTextSize);//文字大小
        paint.setColor(scrollTextColor);//文字颜色
        canvas.drawText(scrollText,rx,50,paint);//绘制文字


        /**
         * 开启线程
         */
        if(thread==null){
            thread=new MyThread();
            thread.start();//开启线程
        }
    }

    /**
     * 线程
     */
    class MyThread extends Thread{
        public void run(){
            while (true){
                rx=rx+2;
                postInvalidate();
                if(rx>getWidth()){
                    rx= (int) -paint.measureText(scrollText);
                }

                /**
                 * 让线程睡眠30毫秒
                 */
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}


 
 

四.使用

aty_my_view_two.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <myview.csdn.com.view.two.ViewTwo
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:scrollTextSize="14dp"
        app:scrollTextColor="@color/colorAccent"
        app:scrollText="这是一个跑马灯"
        >

    </myview.csdn.com.view.two.ViewTwo>

</LinearLayout>




下载资源

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值