您的位置:首页 > 移动开发 > Android开发

android 一些通用View

2015-10-31 15:29 417 查看
1. DividerLineLinearLayout

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();
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: