android开发之自定义组件
自定义Android组件可以帮助我们实现一些特殊需求,而且使得我们的应用变得更加灵活,今天我给大家介绍的是如何自定义一个组件,实现特殊的效果.
package jack.label; import android.app.Activity; import android.os.Bundle; public class RoundRectLabel extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.round_rect_label); } }
package jack.label; import java.util.ArrayList; import java.util.List; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.graphics.Path.Direction; import android.util.AttributeSet; import android.view.View; public class RoundRectLabelView extends View { private Paint mTextPaint; private String mText; private int mAscent = 0; private List<String> textList = new ArrayList<String>(); private int mTextHeight; private int width = 0; private int height = 0; final int border_width = 5; private String mTitle; public RoundRectLabelView(Context context) { super(context); initLabelView(); } public RoundRectLabelView(Context context, AttributeSet attrs) { super(context, attrs); initLabelView(); TypedArray Attributes = context.obtainStyledAttributes(attrs, R.styleable.LabelView); CharSequence s = Attributes.getString(R.styleable.LabelView_text); if (s != null) { setText(s.toString()); } CharSequence title=Attributes.getString(R.styleable.LabelView_titleText); if (title!=null) { setTitle(title.toString()); } setTextColor(Attributes.getColor(R.styleable.LabelView_textColor, 0xFF000000)); int textSize = Attributes.getDimensionPixelOffset( R.styleable.LabelView_textSize, 0); if (textSize > 0) { setTextSize(textSize); } Attributes.recycle(); } public void setText(String text) { mText = text; requestLayout(); invalidate(); } public void setTextSize(int size) { mTextPaint.setTextSize(size); requestLayout(); invalidate(); } private final void initLabelView() { mTextPaint = new Paint(); mTextPaint.setAntiAlias(true); mTextPaint.setTextSize(16); mTextPaint.setColor(0xFF000000); setPadding(3, 3, 3, 3); mTitle=""; } public void setTitle(String title) { mTitle = title; requestLayout(); invalidate(); } public void setTextColor(int color) { mTextPaint.setColor(color); invalidate(); } protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int strCount = 0; textList.removeAll(textList); StringBuilder strBuilder = new StringBuilder(); strBuilder.append(mText); int specMode = MeasureSpec.getMode(widthMeasureSpec); mTextHeight = Math.abs(mTextPaint.getFontMetricsInt().top) + Math.abs(mTextPaint.getFontMetricsInt().bottom) + mTextPaint.getFontMetricsInt().leading; mAscent = (int) (-mTextPaint.ascent() + mTextPaint.descent()); if (specMode == MeasureSpec.EXACTLY) { width = MeasureSpec.getSize(widthMeasureSpec); do { int count = mTextPaint.breakText(strBuilder.substring(0), true, width - 2 * border_width, null); String str = new String(strBuilder.substring(0, count)); textList.add(str); strBuilder.delete(0, count); strCount += count; } while (strCount < mText.length()); } height = textList.size() * mTextHeight + mTextHeight + mTextPaint.getFontMetricsInt().leading; setMeasuredDimension(width, height + 20); } protected void onDraw(Canvas canvas) { final int radius_x = 10; final int radius_y = 10; final int space = 2; final int offset_height = 20; final int offset_one = 30; final int offset_two = offset_one + 10; final int offset_three = offset_two + 10; int text_y = 0; int text_x = 0; Path path = new Path(); RectF rect = new RectF(space, space, width - space, height); path.addRoundRect(rect, radius_x, radius_y, Direction.CW); path.moveTo(space + offset_one, height); path.lineTo(space + offset_two, height + offset_height); path.lineTo(space + offset_three, height); path.close(); mTextPaint.setColor(Color.WHITE); mTextPaint.setStyle(Paint.Style.FILL); canvas.drawPath(path, mTextPaint); mTextPaint.setColor(Color.BLUE); canvas.drawText(mTitle, text_x + 5, text_y + mAscent, mTextPaint); mTextPaint.setColor(Color.GREEN); canvas.drawLine(space, mTextHeight + 1, width - space, mTextHeight + 1, mTextPaint); mTextPaint.setColor(Color.DKGRAY); for (int i = 0; i < textList.size(); i++) { canvas.drawText(textList.get(i), text_x + 5, text_y + mAscent + i * mTextHeight + mTextHeight, mTextPaint); } } }
<!--round_rect_label.xml--> <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/jack.label" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content"> <jack.label.RoundRectLabelView android:background="#7f00" android:layout_width="fill_parent" android:layout_height="wrap_content" app:titleText="我是标题,红色背景的" app:text="文本测试" /> <jack.label.RoundRectLabelView android:background="@drawable/blue" android:layout_width="fill_parent" android:layout_height="wrap_content" app:titleText="我是标题,蓝色背景的" app:text="测试文本中英文混合,自动换行,测试文本中英文混合,自动换行,测试文本中英文混合,自动换行,测试文本中英文混合,自动换行,测试文本中英文混合,自动换行," app:textSize="20dp" /> <jack.label.RoundRectLabelView android:background="@drawable/green" android:layout_width="fill_parent" android:layout_height="wrap_content" app:titleText="我是标题,绿色背景的" app:text="大字体测试" app:textSize="30dp" app:textColor="#ffffffff" /> </LinearLayout> <!--attrs.xml--> <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="TogglePrefAttrs"> <attr name="android:preferenceLayoutChild" /> </declare-styleable> <declare-styleable name="Gallery1"> <attr name="android:galleryItemBackground" /> </declare-styleable> <declare-styleable name="LabelView"> <attr name="text" format="string" /> <attr name="titleText" format ="string"/> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> </declare-styleable> </resources> <!--colors.xml--> <?xml version="1.0" encoding="utf-8"?> <resources> <drawable name="red">#7f00</drawable> <drawable name="blue">#770000ff</drawable> <drawable name="green">#7700ff00</drawable> <drawable name="yellow">#77ffff00</drawable> </resources> <!--strings.xml--> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, RoundRectLabel!</string> <string name="app_name">RoundRectLabel</string> </resources> <!--AndroidManifest.xml--> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jack.label" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".RoundRectLabel" 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>