小视频源码,android之自定义属性
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了小视频源码,android之自定义属性,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2078字,纯文字阅读大概需要3分钟。
内容图文
小视频源码,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之自定义属性所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。