小视频源码,android之自定义属性

小视频源码,android之自定义属性,第1张

概述小视频源码,android之自定义属性相关的代码packagecom.example.test17;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.gr

小视频源码,androID之自定义属性相关的代码

package com.example.test17; import androID.content.Context;import androID.content.res.TypedArray;import androID.graphics.Bitmap;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.graphics.drawable.BitmapDrawable;import androID.graphics.drawable.Drawable;import androID.util.AttributeSet;import androID.vIEw.VIEw; import androIDx.annotation.Nullable; public class MyAttributeVIEw extends VIEw {     private int myAge;    private String myname;    private Bitmap myBg;     public MyAttributeVIEw(Context context, @Nullable AttributeSet attrs) {        super(context, attrs);        //获取属性三种方式        //第一种:通过命名空间        //在(.xml)文件中        //AndroID studio: xmlns:yiqi="http://schemas.androID.com/apk/res-auto"        //eclipse: xmlns:yiqi="http://schemas.androID.com/apk/<包名>"        String age = attrs.getAttributeValue("http://schemas.androID.com/apk/res-auto", "my_age");        String name = attrs.getAttributeValue("http://schemas.androID.com/apk/res-auto", "my_name");        String bg = attrs.getAttributeValue("http://schemas.androID.com/apk/res-auto", "my_bg");         //第二种:遍历属性集合        for (int i = 0; i < attrs.getAttributeCount(); i++) {//            System.out.println(attrs.getAttributename(i) + "==" +attrs.getAttributeValue(i));        }        //第三种:使用系统工具,获取属性        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:                {                    myAge = typedArray.getInt(index,0);                }                    break;                case R.styleable.MyAttributeVIEw_my_name:                    myname = typedArray.getString(index);                    break;                case R.styleable.MyAttributeVIEw_my_bg:                    BitmapDrawable drawable =(BitmapDrawable) typedArray.getDrawable(index);                    myBg = drawable.getBitmap();                    break;            }        }        //记得回收        typedArray.recycle();    }     @OverrIDe    protected voID onDraw(Canvas canvas) {        super.onDraw(canvas);        Paint paint = new Paint();        canvas.drawText(myname+"---"+myAge,50,50,paint);        canvas.drawBitmap(myBg,50,50,paint);    }}

以上就是 小视频源码,androID之自定义属性相关的代码,更多内容欢迎关注之后的文章

总结

以上是内存溢出为你收集整理的小视频源码,android之自定义属性全部内容,希望文章能够帮你解决小视频源码,android之自定义属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1033093.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存