群英传笔记:自定义view一个音频跳动图的绘制
2017-03-06 16:00
399 查看
一个关于view部分方法的介绍
自定义view一个音频跳动图的绘制
代码部分:
public class MyView extends View { public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); } /** * View测量 * EXACTLY:精确模式,指定具体数值。 * AT_MOST:最大模式,此时控件尺寸只要不超过父控件允许的最大值就可。 * UNSPECIFIED:不指定大小测量模式,通常情况下在绘制自定义view才使用。 * view默认onMeasure方法只支持EXACTLY,所以如果自定义如果不重写onMeasure方法 * 控件可以响应指定的具体宽高或者match_parent,如果要支持wrap_content就必须重写来指定wrap的大小 * * @param measure * @return */ private int measureWidth(int measure) { // MeasureSpec是一个32为的int值,使用位运算,是为了提高并优化效率。 int result = 0; // 提取测量模式和大小 int specMode = MeasureSpec.getMode(measure); int specSize = MeasureSpec.getSize(measure); // 通过测量模式给出不同的测量值,当模式为EXACTLY,直接赋值 if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { // 为其它模式时,先设置一个默认值,与默认值比较 result = 200; if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } private int measureHeight(int measure) { int result = 0; // 提取测量模式和大小 int specMode = MeasureSpec.getMode(measure); int specSize = MeasureSpec.getSize(measure); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { result = 200; if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } /** * 绘图 * * @param canvas */ @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher); Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.mipmap.start); canvas.drawBitmap(bitmap, 0, 0, null); canvas.drawBitmap(bitmap1, 0, 0, null); } // 从xml加载组建后回调 @Override protected void onFinishInflate() { super.onFinishInflate(); } // 组件大小改变时回调 @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); } // 回调该方法来确定现实的位置 @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); } // 监听到触摸事件时回调 @Override public boolean onTouchEvent(MotionEvent event) { return super.onTouchEvent(event); } }
自定义view一个音频跳动图的绘制
代码部分:
public class MyRedioBit extends View { private double random; private int mWidth; private int mRectheight; private int mRectWidth; private Paint paint; public MyRedioBit(Context context) { super(context); } public MyRedioBit(Context context, AttributeSet attrs) { super(context, attrs); } public MyRedioBit(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private int mRectCount = 12; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int offset = 10; for (int i = 0; i < mRectCount; i++) { random = Math.random(); float currentHeight = (float) (mRectheight * random); canvas.drawRect((float) (mWidth * 0.4 / 2 + mRectWidth * i + offset) , currentHeight, (float) (mWidth * 0.4 / 2 + mRectWidth * (i + 1)), mRectheight, paint); } // 通知view进行重绘 // invalidate(); // 延迟绘制,每隔300ms进行重绘 postInvalidateDelayed(300); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = getWidth(); mRectheight = getHeight(); mRectWidth = ((int) (mWidth * 0.6 / mRectCount)); // 设置阴影 LinearGradient linearGradient = new LinearGradient(0, 0, mRectWidth, mRectheight, Color.YELLOW, Color.BLUE, Shader.TileMode.CLAMP); paint = new Paint(); paint.setShader(linearGradient); } }
相关文章推荐
- 自定义View,绘制一个圆形,并可以单击拖动
- 自定义View学习篇之绘制一个圆环动画
- Android UI 之居中绘制文本内容的正确方法——实现自定义一个TextView
- Android自定义View——绘制一个会动的时钟
- Android UI 之居中绘制文本内容的正确方法——实现自定义一个TextView
- android自定义View创建一个Path绘制多边形,贝塞尔曲线,
- [Android]自定义绘制一个简易的音频条形图,附上对MP3音频波形数据的采集与展现
- 自定义View之点 线 面以及绘制一个综合应用时钟
- 自定义View学习-绘制一个简单的圆
- Android UI 之居中绘制文本内容的正确方法——实现自定义一个TextView
- 一个自定义View的实例
- 自定义view绘制K线图,点击弹出pop及在第一次进入页面时滑屏弹出pop
- 【Android Trainning 翻译】创建自定义View之创建一个自定义View类
- 创建一个简单的表视图&自定义UITableView的表单元格
- 自定义view中绘制动画
- 【Android Training UI】创建自定义Views(Lesson 1 - 创建一个View类)
- 绘制tableView 自定义section标头
- View:屏幕区域(一个View占据屏幕上的一个矩形区域),负责交互和绘制, 在主线程
- 使用XIB自定义一个UIView,然后将这个view添加到controller的view 上(相当于所有界面都通过xib来实现)