自定义View
来源:互联网 发布:黄金价格数据库 编辑:程序博客网 时间:2024/06/05 14:59
PathActivity
PathEffect来定义绘制效果,PathEffect有如下几个子类,没一个子类代表一种效果,然后通过Canvas里onDraw的方法沿着路径绘制图形。
ComposePathEffect
CornerPathEffect
DashPathEffect
DiscretePathEffect
PathDashPathEffect
SumPathEffect
package com.android.xiong.gridlayoutTest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ComposePathEffect;
import android.graphics.CornerPathEffect;
import android.graphics.DashPathEffect;
import android.graphics.DiscretePathEffect;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathDashPathEffect;
import android.graphics.PathEffect;
import android.graphics.SumPathEffect;
import android.os.Bundle;
import android.view.View;
public class PathActivity extends Activity {
/**
*
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
class MyView extends View{
float phase;
//七种不同的效果
PathEffect[] effects=new PathEffect[7];int[] colors;
private Paint paint;
Path path;
public MyView(Context context) {
super(context);
// 创建path并初始化
paint=new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
path=new Path();
path.moveTo(0, 0);
for (int i = 1; i <=40; i++) {
//生成40个点,随机生成y轴坐标并练成一个path
path.lineTo(i*20, (float) (Math.random()*60));
//初始化7个颜色
colors=new int[]{
Color.BLACK,Color.BLUE,Color.DKGRAY,Color.GREEN,
Color.YELLOW,Color.RED,Color.MAGENTA}
;
}
}
@Override
protected void onDraw(Canvas canvas) {
//将背景颜色填充为白色
canvas.drawColor(Color.WHITE);
//不使用路径效果
effects[0]=null;
//使用CornerPathEffect
effects[1]=new CornerPathEffect(10);
//初始化DiscretePathEffect
effects[2]=new DiscretePathEffect(3.0f, 5.0f);
//
effects[3]=new DashPathEffect(new float[]{20, 10,5,10},phase);
//初始化PthDashPathEffect
Path p=new Path();
p.addRect(0, 0, 8, 8, Path.Direction.CCW);
effects[4]=new PathDashPathEffect(p, 12, phase, PathDashPathEffect.Style.ROTATE);
//初始化ComposePathEffect
effects[5]=new ComposePathEffect(effects[2], effects[4]);
effects[6]=new SumPathEffect(effects[4], effects[3]);
//将画布移动到(8,8)处开始绘制
//使用7种不同的路径效果,7种不同的颜色来绘制路径
for (int i = 0; i < effects.length; i++) {
paint.setPathEffect(effects[i]);
paint.setColor(colors[i]);
canvas.drawPath(path, paint);
canvas.translate(0, 60);
//转化60度
}
phase+=1;
//改变phase值,形成动画效果
invalidate();
//super.onDraw(canvas);
}
}
}
运行效果图如下
0 0
- 自定义view
- 自定义View
- 自定义view
- 自定义View
- 自定义View
- 自定义view
- 自定义View
- 自定义view
- 自定义view
- 自定义View
- 自定义View
- 自定义view
- 自定义view
- 自定义view
- 自定义view
- 自定义view
- 自定义View
- 自定义View
- PyCharm安装及使用(一)
- eclipse中调出logcat
- Swift语法之 ---- ?和!区别
- java web 导出PDF聊天记录
- web.xml
- 自定义View
- Mysql用户无法远程连接问题
- UESTC 30 最短路
- Microsoft Office MIME types
- @使转移字符无效
- 四、Android情景分析之深入解析system_server
- [leetcode] 101. Symmetric Tree
- spring学习之springMVC 返回类型选择 以及 SpringMVC中model,modelMap.request,session取值顺序
- sklearn中常用数据预处理方法