Android自定义View 属性添加

 

昨天写的自定义View,所有的属性都是写死的,拓展性基本为零。今天讲解自定义View属性的设置,首先在res的values文件夹下创建一个attrs.xml文件。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="ScrollTextView">
       <attr name="textSize" format="dimension"></attr>
       <attr name="textColor" format="color"></attr>
       <attr name="textString" format="string"></attr>
       <attr name="scrollVelocity" format="enum">
           <enum name="slow" value="1"></enum>
           <enum name="midd" value="2"></enum>
           <enum name="fast" value="3"></enum>
       </attr>
   </declare-styleable>
</resources>


这里添加了四个属性,其中scrollVelocity设置为枚举类型,有三个属性值。在自定义控件里要获取这些属性值,自定义View代码如下:


public class ScrollTextView extends View {
private Paint paint;
   private int x = 0;
   private MyThread myThread;
   private int textSize;
   private int color;
   private int value;
   private int speed;
   private String text;

   public ScrollTextView(Context context) {
this(context, null);
       init();
   }

public ScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
       TypedArray ta = context.obtainStyledAttributes(attrs,
               R.styleable.ScrollTextView);
       textSize = (int) ta.
getDimension(R.styleable.ScrollTextView_textSize, 45);
       color = ta.
getColor(R.styleable.ScrollTextView_textColor, Color.RED);
       value = ta.
getInt(R.styleable.ScrollTextView_scrollVelocity, 0);
       text = ta.
getString(R.styleable.ScrollTextView_textString);
       if (value == 1) {
speed = 1;
       } else if (value == 2) {
speed = 2;
       } else {
speed = 3;
       }
init();
       ta.recycle();
   }

private void init() {
paint = new Paint();
       paint.setTextSize(textSize);
       paint.setColor(color);
       if (myThread == null) {
myThread = new MyThread();
           myThread.start();
       }
}

@Override
   protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
       canvas.drawText(text, x, 70, paint);
   }

class MyThread extends Thread {
@Override
       public void run() {
while (true) {
x += speed;
               if (x > getWidth()) {
x = (int) -paint.measureText(text);
               }
postInvalidate();
               try {
Thread.sleep(5);
               } catch (InterruptedException e) {
e.printStackTrace();
               }
}
}
}
}


在构造方法里调用obtainStyledAttributes方法得到TypeArray对象,就可以以此获得各个属性值了。

在activity_main.xml里调用这个控件和添加自定义的属性,如下:


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:yayun="http://schemas.android.com/apk/res-auto"
   android:id="@+id/activity_main"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <ad.scrolltextview.ScrollTextView
       android:layout_width="match_parent"
       android:layout_height="60dp"
       yayun:scrollVelocity="slow"
       yayun:textColor="@color/colorPrimaryDark"
       yayun:textSize="80px"
       yayun:textString="TANGTANGTANG" />
</RelativeLayout>


MainActivity.java代码如下:

public class MainActivity extends AppCompatActivity {

@Override
   protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
   }
}


运行实例如下:

 

 

 

 

可以在布局文件中修改属性如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:yayun="http://schemas.android.com/apk/res-auto"
   android:id="@+id/activity_main"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <ad.scrolltextview.ScrollTextView
       android:layout_width="match_parent"
       android:layout_height="60dp"
       yayun:scrollVelocity="fast"
       yayun:textColor="@color/colorAccent"
       yayun:textSize="40px"
       yayun:textString="TANGBAOBAO" />
</RelativeLayout>


再次运行实例如下:


 


 


如果您喜欢,请转发至朋友圈,在此感谢。

 


发布了498 篇原创文章 · 获赞 115 · 访问量 126万+

猜你喜欢

转载自blog.csdn.net/yayun0516/article/details/72858344