android 一些通用View
2015-10-31 15:29
417 查看
1. DividerLineLinearLayout
2.三角形 View TriangleArrowTextView
import android.content.Context ; import android.content.res.TypedArray ; import android.graphics.Canvas ; import android.graphics.drawable.Drawable ; import android.util.AttributeSet ; import android.view.View ; import android.widget.LinearLayout ; /** * Created by whuthm * 分隔线线性布局 */ public class DividerLineLinearLayout extends LinearLayout { private int mDividerTopMargin; private int mDividerBottomMargin ; private int mDividerLeftMargin ; private int mDividerRightMargin ; private Drawable mDividerDrawable; private int mDividerWidth ; private int mDividerHeight ; private boolean mDrawStart ; private boolean mDrawEnd ; public DividerLineLinearLayout(Context context) { this (context, null) ; } public DividerLineLinearLayout(Context context , AttributeSet attrs) { super (context, attrs) ; setWillNotDraw( false); final TypedArray a = context.obtainStyledAttributes(attrs , R.styleable.DividerLinearLayout) ; mDividerDrawable = a.getDrawable(R.styleable.DividerLinearLayout_android_divider ); mDividerWidth = a.getDimensionPixelSize( R.styleable.DividerLinearLayout_dividerWidth, 0 ); mDividerHeight = a.getDimensionPixelSize( R.styleable.DividerLinearLayout_android_dividerHeight, 0 ); final int horMargin = a.getDimensionPixelSize( R.styleable.DividerLinearLayout_dividerHorMargin, 0 ); final int verMargin = a.getDimensionPixelSize( R.styleable.DividerLinearLayout_dividerVerMargin, 0 ); mDividerTopMargin = a.getDimensionPixelSize( R.styleable.DividerLinearLayout_dividerTopMargin, verMargin) ; mDividerBottomMargin = a.getDimensionPixelSize( R.styleable.DividerLinearLayout_dividerBottomMargin, verMargin) ; mDividerLeftMargin = a.getDimensionPixelSize( R.styleable.DividerLinearLayout_dividerLeftMargin, horMargin) ; mDividerRightMargin = a.getDimensionPixelSize( R.styleable.DividerLinearLayout_dividerRightMargin, horMargin) ; mDrawStart = a.getBoolean(R.styleable.DividerLinearLayout_drawStart, true) ; mDrawEnd = a.getBoolean(R.styleable.DividerLinearLayout_drawEnd, true) ; a.recycle() ; } @Override protected void dispatchDraw(Canvas canvas) { super .dispatchDraw(canvas); final int orientation = getOrientation() ; final Drawable d = mDividerDrawable; final int width = getWidth() ; final int height = getHeight() ; if (d != null && width > 0 && height > 0) { if (orientation == HORIZONTAL && mDividerWidth > 0 ) { drawHorizontalDividerLine(canvas) ; } else if (orientation == VERTICAL && mDividerHeight > 0 ) { drawVerticalDividerLine(canvas) ; } } } private void drawHorizontalDividerLine(Canvas canvas) { final Drawable d = mDividerDrawable; final int width = getWidth() ; final int height = getHeight() ; int left ; int right ; int top = mDividerTopMargin; int bottom = height - mDividerBottomMargin; if (bottom <= top) { return; } int count = getChildCount(); if (count > 0 && mDrawStart ) { left = 0 ; right = left + mDividerWidth; d.setBounds(left , top, right, bottom) ; d.draw(canvas) ; } for (int i = 0; i < count - 1; i++) { View child = getChildAt(i); if (child.getVisibility() == View. VISIBLE) { left = child.getRight(); right = left + mDividerWidth ; d.setBounds(left, top, right , bottom); d.draw(canvas); } } if (count > 0 && mDrawEnd) { left = width - mDividerWidth; right = left + mDividerWidth; d.setBounds(left , top, right, bottom) ; d.draw(canvas) ; } } private void drawVerticalDividerLine(Canvas canvas) { final Drawable d = mDividerDrawable; final int width = getWidth() ; final int height = getHeight() ; int left = mDividerLeftMargin; int right = width - mDividerRightMargin; int top ; int bottom ; if (right <= left) { return; } int count = getChildCount(); if (count > 0 && mDrawStart ) { top = 0 ; bottom = top + mDividerHeight; d.setBounds(left , top, right, bottom) ; d.draw(canvas) ; } for (int i = 0; i < count - 1; i++) { View child = getChildAt(i); if (child.getVisibility() == View. VISIBLE) { top = child.getBottom(); bottom = top + mDividerHeight ; d.setBounds(left, top, right , bottom); d.draw(canvas); } } if (count > 0 && mDrawEnd) { top = height - mDividerHeight; bottom = top + mDividerHeight; d.setBounds(left , top, right, bottom) ; d.draw(canvas) ; } } }attrs
<!--Divider Line--> <declare-styleable name="DividerLinearLayout"> <attr name= "android:dividerHeight" /> <attr name= "android:divider" /> <attr name= "dividerWidth" /> <attr name= "dividerTopMargin" format="reference|dimension" /> <attr name= "dividerBottomMargin" format="reference|dimension" /> <attr name= "dividerLeftMargin" format="reference|dimension" /> <attr name= "dividerRightMargin" format="reference|dimension" /> <attr name= "dividerHorMargin" format="reference|dimension" /> <attr name= "dividerVerMargin" format="reference|dimension" /> <attr name= "drawStart" format="boolean" /> <attr name= "drawEnd" format="boolean" /> </declare-styleable>
2.三角形 View TriangleArrowTextView
import android.content.Context ; import android.graphics.Canvas ; import android.graphics.Path ; import android.graphics.drawable.ColorDrawable ; import android.graphics.drawable.Drawable ; import android.util.AttributeSet ; import android.widget.TextView ; /** * Created by whuthm * 父控件必须android:clipChildren="false" */ public class TriangleArrowTextView extends TextView { private int mTriangleWidth; public TriangleArrowTextView(Context context) { super (context); init() ; } public TriangleArrowTextView(Context context , AttributeSet attrs) { super (context, attrs) ; init() ; } public TriangleArrowTextView(Context context , AttributeSet attrs, int defStyle) { super (context, attrs , defStyle); init() ; } private void init() { mTriangleWidth = getResources().getDimensionPixelSize(R.dimen. triangle_width); mTriangleWidth = mTriangleWidth - mTriangleWidth % 2 ; } @Override protected void onDraw(Canvas canvas) { super .onDraw(canvas); int width = getWidth() ; int l = width / 20; if ( mTriangleWidth <= 0 || l + mTriangleWidth > width) { return; } int halfWidth = mTriangleWidth / 2; Drawable background = getBackground() ; if (background instanceof ColorDrawable) { Drawable.ConstantState constantState = background.getConstantState() ; if (constantState != null) { Drawable d = constantState.newDrawable(getResources()) ; if (d instanceof ColorDrawable) { ColorDrawable colorDrawable = (ColorDrawable) d ; Path path = new Path(); path.reset(); path.moveTo(l, 0); // 此点为多边形的起点 path.lineTo(l + halfWidth, -halfWidth); path.lineTo(l + mTriangleWidth , 0) ; path.close(); // 使这些点构成封闭的多边形 canvas.save(); canvas.clipPath(path); colorDrawable.setBounds(l, -halfWidth, l + mTriangleWidth, 0 ); colorDrawable.draw(canvas); canvas.restore(); } } } } }
相关文章推荐
- Android下快速提取ndk crash日志的命令
- Android 开发基础系列——程序崩溃之Unable to instantiate activity ComponentInfo
- Android Scroller 的简单用法
- android Acitivity之间的数据传递
- Android——android studio 安装完成之后导入项目的乱码问题
- Android设计模式之单例模式(一)
- Android触摸屏事件派发机制详解与源码分析一(View篇)
- android utils
- Android开发岗位要求集锦
- Android有趣的爆炸散落动画view:开源ExplosionField
- Android Fragment 你应该知道的一切
- android系统音量监控
- Android性能优化典范 - 第3季
- Android性能优化典范 - 第2季
- 在Android studio中进行NDK开发
- Android性能优化典范
- Android获取通讯录信息
- Android适配点及影响点
- Android编程开发中ListView的常见用法分析
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)