自定义view(13)自定义属性
1.添加attrs.xml文件
在android studio下,在res/values 下新建资源文件attrs.xml
2.添加自定义的属性
在attrs.xml中添加属性,如下。其中format是属性的类型,如float,color,boolean,dimension
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 <declare-styleable name="thermograph"> 4 <attr name="minValue" format="float" /> 5 <attr name="maxValue" format="float" /> 6 <attr name="ratio" format="float" /> 7 <attr name="hotValue" format="float" /> 8 <attr name="coldValue" format="float" /> 9 <attr name="thermoValue" format="float" /> 10 <attr name="textSize" format="dimension" /> 11 <attr name="warningColor" format="color" /> 12 <attr name="contourColor" format="color" /> 13 <attr name="textColor" format="color" /> 14 <attr name="borderColor" format="color" /> 15 <attr name="hotColor" format="color" /> 16 <attr name="coldColor" format="color" /> 17 <attr name="valueColor" format="color" /> 18 <attr name="showValue" format="boolean" /> 19 20 </declare-styleable> 21 </resources>
3.在布局文件中使用自定义的属性
3.1 在布局文件根结点中添加命名空间。
1 <?xml version="1.0" encoding="utf-8"?> 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:thermograph="http://schemas.android.com/apk/res-auto" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="@color/colorMainBackground" 8 >
3.2 使用自定义的属性
1 <?xml version="1.0" encoding="utf-8"?> 2 <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:thermograph="http://schemas.android.com/apk/res-auto" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:background="@color/colorMainBackground" 8 > 9 10 <com.cnblogs.sjjg.tempview.Thermograph 11 android:id="@+id/thermograph1" 12 android:layout_width="10dp" 13 android:layout_height="30dp" 14 android:layout_marginStart="8dp" 15 android:layout_marginLeft="8dp" 16 android:layout_marginEnd="8dp" 17 android:layout_marginRight="8dp" 18 app:layout_constraintBottom_toBottomOf="@+id/thermograph2" 19 app:layout_constraintEnd_toStartOf="@+id/thermograph2" 20 app:layout_constraintStart_toStartOf="parent" 21 app:layout_constraintTop_toTopOf="@+id/thermograph2" 22 thermograph:borderColor="@color/temp1_color" 23 thermograph:coldValue="10" 24 thermograph:hotValue="50" 25 thermograph:maxValue="70" 26 thermograph:minValue="-30" /> 27 28 <com.cnblogs.sjjg.tempview.Thermograph 29 android:id="@+id/thermograph2" 30 android:layout_width="100dp" 31 android:layout_height="250dp" 32 android:layout_marginStart="8dp" 33 android:layout_marginLeft="8dp" 34 android:layout_marginTop="32dp" 35 android:layout_marginEnd="8dp" 36 android:layout_marginRight="8dp" 37 app:layout_constraintEnd_toEndOf="parent" 38 app:layout_constraintHorizontal_bias="0.5" 39 app:layout_constraintStart_toStartOf="parent" 40 app:layout_constraintTop_toTopOf="parent" 41 thermograph:coldValue="0" 42 thermograph:hotValue="50" 43 thermograph:maxValue="70" 44 thermograph:minValue="-30" /> 45 46 <SeekBar 47 android:id="@+id/seekBar" 48 android:layout_width="0dp" 49 android:layout_height="24dp" 50 android:layout_marginStart="8dp" 51 android:layout_marginLeft="8dp" 52 android:layout_marginTop="32dp" 53 android:layout_marginEnd="8dp" 54 android:layout_marginRight="8dp" 55 android:max="100" 56 android:progress="50" 57 app:layout_constraintEnd_toEndOf="parent" 58 app:layout_constraintStart_toStartOf="parent" 59 app:layout_constraintTop_toBottomOf="@+id/thermograph2" /> 60 61 </androidx.constraintlayout.widget.ConstraintLayout>
4.在自定义的view中取属性值
1 private void init(Context context,AttributeSet attrs){ 2 3 paint = new Paint(Paint.ANTI_ALIAS_FLAG); 4 paint.setStyle(Paint.Style.FILL); 5 6 setLayerType(View.LAYER_TYPE_SOFTWARE,paint); 7 8 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.thermograph); 9 min = ta.getFloat(R.styleable.thermograph_minValue, -50); 10 max = ta.getFloat(R.styleable.thermograph_maxValue,150); 11 value = ta.getFloat(R.styleable.thermograph_thermoValue,0); 12 hot = ta.getFloat(R.styleable.thermograph_hotValue,60); 13 cold = ta.getFloat(R.styleable.thermograph_coldValue,0); 14 ratio = ta.getFloat(R.styleable.thermograph_ratio,0.25f); 15 16 txtColor = ta.getColor(R.styleable.thermograph_textColor,Color.WHITE); 17 18 int c = ContextCompat.getColor(context,R.color.temp_color); 19 borderColor = ta.getColor(R.styleable.thermograph_borderColor,c); 20 21 c = ContextCompat.getColor(context,R.color.temp_cold_color); 22 contourColor = ta.getColor(R.styleable.thermograph_contourColor,c); 23 24 c = ContextCompat.getColor(context,R.color.temp_hot_color); 25 hotColor = ta.getColor(R.styleable.thermograph_hotColor,c); 26 27 c = ContextCompat.getColor(context,R.color.temp_cold_color); 28 coldColor = ta.getColor(R.styleable.thermograph_coldColor,c); 29 30 showValue = ta.getBoolean(R.styleable.thermograph_showValue,true); 31 32 } 33 34 public Thermograph(Context context) { 35 super(context); 36 init(context,null); 37 } 38 39 public Thermograph(Context context, @Nullable AttributeSet attrs) { 40 super(context, attrs); 41 init(context,attrs); 42 } 43 44 public Thermograph(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { 45 super(context, attrs, defStyleAttr); 46 init(context,attrs); 47 } 48 49 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 50 public Thermograph(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { 51 super(context, attrs, defStyleAttr, defStyleRes); 52 init(context,attrs); 53 }