Android开发-自定义View-AndroidStudio(八)自定义View初体验

时间:2022-10-27 15:53:13
转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53783417
觉得博文有用,请点赞,请评论,请关注,谢谢!~
老规矩,先上效果图,看个效果,如果符合你的项目或者确定你要了解的内容,再往下看吧:
Android开发-自定义View-AndroidStudio(八)自定义View初体验
MainActivity.java:
package com.iwanghang.autoattribute;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
MyAttributeView.java:
package com.iwanghang.autoattribute;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

public class MyAttributeView extends View {

    private int age;
    private String name;
    private int text_size;
    private Bitmap bg;
    private Bitmap bg2;

    public MyAttributeView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // 获取属性三种方式

        // 1、用命名空间获取
        String my_name = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_name");
        String my_age = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_age");
        String my_bg = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","my_bg");
        System.out.println("ceshi 用命名空间获取 = my_name = "+ my_name + ", my_age = "+ my_age + ", my_bg = "+ my_bg);

        // 2、遍历属性集合
        for (int i = 0; i <attrs.getAttributeCount() ; i++) {
            System.out.println("ceshi 遍历属性集合 = " + attrs.getAttributeName(i) + " | " + attrs.getAttributeValue(i));
        }

        // 3、使用系统工具,获取属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MyAttributeView);
        for (int i = 0; i <typedArray.getIndexCount() ; i++) {
            int index = typedArray.getIndex(i);

            switch (index){
                case R.styleable.MyAttributeView_my_age:
                    age = typedArray.getInt(index,0);
                    break;
                case R.styleable.MyAttributeView_my_name:
                    name = typedArray.getString(index);
                    System.out.println("ceshi2 name = " + name);
                    break;
                case R.styleable.MyAttributeView_text_size:
                    text_size = typedArray.getDimensionPixelSize(index, 0);
                    break;
                case R.styleable.MyAttributeView_my_bg:
                    Drawable drawable = typedArray.getDrawable(index);
                    BitmapDrawable drawable1 = (BitmapDrawable) drawable;
                    bg = drawable1.getBitmap();
                    break;
                case R.styleable.MyAttributeView_my_bg_2:
                    Drawable drawable3 = typedArray.getDrawable(index);
                    BitmapDrawable drawable4 = (BitmapDrawable) drawable3;
                    bg2 = drawable4.getBitmap();
                    break;
            }
        }

        // 记得回收
        typedArray.recycle();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        if (text_size != 0) {
            // Layout中如果设置字体大小,则设置为Layout中字体大小
            paint.setTextSize(text_size);
        }else {
            // Layout中如果没有设置字体大小,则20sp
            paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 20, getResources().getDisplayMetrics()));
        }
        canvas.drawText(name,50,50,paint);
        canvas.drawText(String.valueOf(age),50,100,paint);


        //canvas.drawBitmap(bg,50,150,paint);
        canvas.drawBitmap(bg,null,new Rect(0,0,650,500),null);
        canvas.drawBitmap(bg2,null,new Rect(100,450,550,750),null);
    }

}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:iwanghang="http://schemas.android.com/apk/res-auto"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.iwanghang.autoattribute.MainActivity">

    <com.iwanghang.autoattribute.MyAttributeView
        iwanghang:my_age="100"
        iwanghang:my_name="_iwanghang_"
        iwanghang:text_size="30sp"
        iwanghang:my_bg="@drawable/christmas"
        iwanghang:my_bg_2="@drawable/c2"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!--如果删除这句,默认字体大小为20sp-->
    <!--iwanghang:text_size="30sp"-->
</RelativeLayout>
attrs.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyAttributeView">
        <attr name="my_age" format="integer"/>
        <attr name="my_name" format="string" />
        <attr name="text_size" format="dimension" />
        <attr name="my_bg" format="reference|color"/>
        <attr name="my_bg_2" format="reference|color"/>
    </declare-styleable>
</resources>


转载请注明出处: http://blog.csdn.net/iwanghang/article/details/53783417



欢迎移动开发爱好者交流
沈阳或周边城市公司有意开发Android,请与我联系
联系方式
Android开发-自定义View-AndroidStudio(八)自定义View初体验
微信:iwanghang
QQ:413711276
邮箱:iwanghang@qq.com



觉得博文有用,请点赞,请评论,请关注,谢谢!~