一、自定义View,使文字横向循环滚动
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cctvjiatao.customview" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="22" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>com.cctvjiatao.customview.MainActivity
package com.cctvjiatao.customview; import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <com.cctvjiatao.customview.v1.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:visibility="gone"> </com.cctvjiatao.customview.v1.CustomView1> <com.cctvjiatao.customview.v2.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </com.cctvjiatao.customview.v2.CustomView1> <com.cctvjiatao.customview.v2.CustomView2 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </com.cctvjiatao.customview.v2.CustomView2> <com.cctvjiatao.customview.v2.CustomView3 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"> </com.cctvjiatao.customview.v2.CustomView3> <com.cctvjiatao.customview.v3.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent"> </com.cctvjiatao.customview.v3.CustomView1> </FrameLayout>com.cctvjiatao.customview.v3.CustomView1
package com.cctvjiatao.customview.v3; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; /** * @作者: jiatao * @修改时间:2016-3-12 下午4:20:42 * @包名:com.cctvjiatao.customview.v3 * @文件名:CustomView1.java * @版权声明:www.cctvjiatao.com * @功能: 自定义View,使文字横向循环滚动 */ public class CustomView1 extends View { private float move_x = 0; private Paint paint = new Paint(); private MoveThread moveThread; public CustomView1(Context context, AttributeSet attrs) { super(context, attrs); } public CustomView1(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { paint.setTextSize(30); canvas.drawText("cctvjiatao", move_x, 30, paint); if (moveThread == null) { moveThread = new MoveThread(); moveThread.start(); } } class MoveThread extends Thread { @Override public void run() { while (true) { move_x += 3; if (move_x > getWidth()) {//如果文字滑出屏幕 move_x = 0 - paint.measureText("cctvjiatao"); // move_x = 0;//对比效果差异 // move_x = paint.measureText("cctvjiatao");//对比效果差异 } postInvalidate();//更新View try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
二、自定义View,循环画圆
AndroidManifest.xml 同一
com.cctvjiatao.customview.MainActivity 同一
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" > <com.cctvjiatao.customview.v1.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00ff00" android:visibility="gone" > </com.cctvjiatao.customview.v1.CustomView1> <com.cctvjiatao.customview.v2.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" > </com.cctvjiatao.customview.v2.CustomView1> <com.cctvjiatao.customview.v2.CustomView2 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" > </com.cctvjiatao.customview.v2.CustomView2> <com.cctvjiatao.customview.v2.CustomView3 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" > </com.cctvjiatao.customview.v2.CustomView3> <com.cctvjiatao.customview.v3.CustomView1 android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone" > </com.cctvjiatao.customview.v3.CustomView1> <com.cctvjiatao.customview.v3.CustomView2 android:layout_width="match_parent" android:layout_height="match_parent"> </com.cctvjiatao.customview.v3.CustomView2> </FrameLayout>com.cctvjiatao.customview.v3.CustomView2
package com.cctvjiatao.customview.v3; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; /** * @作者: jiatao * @修改时间:2016-3-12 下午4:42:36 * @包名:com.cctvjiatao.customview.v3 * @文件名:CustomView2.java * @版权声明:www.cctvjiatao.com * @功能: 自定义View,循环画圆,圆的形状有 RectF决定 */ public class CustomView2 extends View { private ArcThread arcThread; private float sweepAngle = 0; private RectF rectf1 = new RectF(0 ,60 ,100, 160); private RectF rectf2 = new RectF(150 ,60 ,250, 130); private RectF rectf3 = new RectF(350 ,60 ,450, 200); public CustomView2(Context context) { super(context); } public CustomView2(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onDraw(Canvas canvas) { Paint paint1 = new Paint(); paint1.setColor(0xff00ff00); Paint paint2 = new Paint(); paint2.setColor(0xffff0000); Paint paint3 = new Paint(); paint3.setColor(0xff0000ff); /** * canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint) * oval :指定圆弧的外轮廓矩形区域 * startAngle: 圆弧起始角度,单位为度 * sweepAngle: 圆弧扫过的角度,顺时针方向,单位为度 * useCenter: 如果为True时,在绘制圆弧时将圆心包括在内,通常用来绘制扇形 * paint: 绘制圆弧的画板属性,如颜色,是否填充等 * */ canvas.drawArc(rectf1, 0, sweepAngle, true, paint1); canvas.drawArc(rectf2, 0, sweepAngle, true, paint2); canvas.drawArc(rectf3, 0, sweepAngle, true, paint3); if(arcThread == null){ arcThread = new ArcThread(); arcThread.start(); } } class ArcThread extends Thread{ @Override public void run() { while(true){ sweepAngle ++; if(sweepAngle > 360){ sweepAngle = 0; } postInvalidate(); try { Thread.sleep(30); } catch (InterruptedException e) { e.printStackTrace(); } } } } }效果图: