Android-自定義屬性

在Android開發中,大多數都是用Android提供的屬性,例如:android

android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="40dp"

這些都是Android定義的,只是在使用Android定義的屬性,如今咱們本身來自定義屬性canvas

 

在自定義屬性以前,先去了解Android是如何自定義屬性的:須要找到SDK目錄中(D:\tools\sdk\platforms\android-28\data\res\values)ide

attrs.xml裏面就是定義了Android的屬性規則:ui

name爲屬性名稱,format爲類型spa

 

本身自定義屬性:3d

myattribute:my_age="26"code

myattribute:my_name="刀郎"orm

myattribute:my_bg="@mipmap/jtx"xml

注意:須要申請:xmlns:myattribute="http://schemas.android.com/apk/res-auto"blog

<!-- 自定義屬性 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:myattribute="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context=".ShangGuiguTestActivity">

    <view.custom.shangguigucustomview.MyCustomAttribute
        myattribute:my_age="26"
        myattribute:my_name="刀郎"
        myattribute:my_bg="@mipmap/jtx"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

 

 編寫 attrs.xml文件,來規則屬性類型:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyCustomAttribute">

        <attr name="my_age" format="integer" />
        <attr name="my_name" format="string" />
        <attr name="my_bg" format="reference" />

    </declare-styleable>

</resources>

 

使用自定義屬性:

public class MyCustomAttribute extends View {

    private static final String TAG  = MyCustomAttribute.class.getSimpleName();

    private Paint paint;

    public MyCustomAttribute(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
        paint.setAntiAlias(true); // 去鋸齒
        paint.setTextSize(60);

        initView(context, attrs);
    }

    private int myage;
    private String myname;
    private Drawable mybg;

    private void initView(Context context, AttributeSet attributeSet) {

        // 1.經過命名控件來獲取
        /*String age = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_age");
        String name = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_name");
        String bg = attributeSet.getAttributeValue("http://schemas.android.com/apk/res-auto", "my_bg");

        Log.i(TAG, "age:" + age + " name:" + name + " bg:" + bg);*/

        // 2.經過變量屬性方式打印獲取
        /*for (int i=0; i<attributeSet.getAttributeCount(); i++) {
            Log.i(TAG, "name:" + attributeSet.getAttributeName(i) + " value:" + attributeSet.getAttributeValue(i));
        }*/

        // 3.經過控件方式來獲取,比較靠譜,這種方式才能夠把圖片顯示
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.MyCustomAttribute);
        myage = typedArray.getInt(R.styleable.MyCustomAttribute_my_age, 0);
        myname = typedArray.getString(R.styleable.MyCustomAttribute_my_name);
        mybg = typedArray.getDrawable(R.styleable.MyCustomAttribute_my_bg);

        typedArray.recycle(); // 由於源碼中是進行回收的

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        // 測量 寬度  高度
        setMeasuredDimension(1200, 3000);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 繪製個人年齡相關信息
        canvas.drawText(String.valueOf(myage), 60, 100, paint);

        // 繪製個人名稱相關信息
        canvas.drawText(myname, 60, 180, paint);

        // 繪製圖片
        canvas.drawBitmap(((BitmapDrawable)mybg).getBitmap(), 60, 250, paint);
    }
}

 

 效果圖:

相關文章
相關標籤/搜索