androiddeclare
① android 自定義view 怎麼規定view的樣式
android 自定義view的樣式的實現:
1.在values文件夾下,打開attrs.xml,其實這個文件名稱可以是任意的,寫在這里更規范一點,表示裡面放的全是view的屬性。
2.因為我們下面的實例會用到2個長度,一個顏色值的屬性,所以我們這里先創建3個屬性。
<declare-styleable name="rainbowbar">
<attr name="rainbowbar_hspace" format="dimension"></attr>
<attr name="rainbowbar_vspace" format="dimension"></attr>
<attr name="rainbowbar_color" format="color"></attr>
</declare-styleable>
舉例說明:
藍色的進度條
public class RainbowBar extends View {
//progress bar color
int barColor = Color.parseColor("#1E88E5");
//every bar segment width
int hSpace = Utils.dpToPx(80, getResources());
//every bar segment height
int vSpace = Utils.dpToPx(4, getResources());
//space among bars
int space = Utils.dpToPx(10, getResources());
float startX = 0;
float delta = 10f;
Paint mPaint;
public RainbowBar(Context context) {
super(context);
}
public RainbowBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RainbowBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//read custom attrs
TypedArray t = context.obtainStyledAttributes(attrs,
R.styleable.rainbowbar, 0, 0);
hSpace = t.getDimensionPixelSize(R.styleable.rainbowbar_rainbowbar_hspace, hSpace);
vSpace = t.getDimensionPixelOffset(R.styleable.rainbowbar_rainbowbar_vspace, vSpace);
barColor = t.getColor(R.styleable.rainbowbar_rainbowbar_color, barColor);
t.recycle(); // we should always recycle after used
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(barColor);
mPaint.setStrokeWidth(vSpace);
}
.......
}
View有了三個構造方法需要我們重寫,這里介紹下三個方法會被調用的場景,
第一個方法,一般我們這樣使用時會被調用,View view = new View(context);
第二個方法,當我們在xml布局文件中使用View時,會在inflate布局時被調用,
<View layout_width="match_parent" layout_height="match_parent"/>。
第三個方法,跟第二種類似,但是增加style屬性設置,這時inflater布局時會調用第三個構造方法。
<View style="@styles/MyCustomStyle" layout_width="match_parent" layout_height="match_parent"/>。
② Android自定義字母導航欄
自定義側邊字母導航欄,根據實際字母高度進行顯示
先上效果圖
public class SlideBar extends View {
//當前手指滑動到的位置
private int choosedPosition = -1;
//畫文字的畫筆
private Paint paint;
//單個字母的高度
private float perTextHeight;
//字母的字體大小
private float letterSize;
//字母的垂直間距
private float letterGap;
//字母圓形背景半徑
private float bgRadius;
private ArrayList<String> firstLetters = new ArrayList<>();
//繪制點擊時的藍色背景
private Paint backgroundPaint;
private Context context;
private OnTouchFirstListener listener;
public RecyclerView getTiku_recycle_answer() {
return tiku_recycle_answer;
}
public void setTiku_recycle_answer(RecyclerView tiku_recycle_answer) {
this.tiku_recycle_answer = tiku_recycle_answer;
}
RecyclerView tiku_recycle_answer;
public SlideBar(Context context) {
this(context, null);
}
public SlideBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlideBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SlideBar);
//字母的字體大小
letterSize = typedArray.getDimension(R.styleable.SlideBar_letter_size, DisplayUtils.sp2px(context, 10.0f));
//每個字母的高
perTextHeight = typedArray.getDimension(R.styleable.SlideBar_letter_height, DisplayUtils.dp2px(context, 10.0f));
//字母垂直間距
letterGap = typedArray.getDimension(R.styleable.SlideBar_letter_gap, DisplayUtils.dp2px(context, 6.0f));
//字母垂直間距
bgRadius = typedArray.getDimension(R.styleable.SlideBar_letter_bg_radius, DisplayUtils.dp2px(context, 8.0f));
typedArray.recycle();
init();
}
public void init() {
//初始化畫筆
paint = new Paint();
paint.setAntiAlias(true);
paint.setTextSize(letterSize);
paint.setTypeface(Typeface.DEFAULT_BOLD);
//初始化圓形背景畫筆
backgroundPaint = new Paint();
backgroundPaint.setAntiAlias(true);
backgroundPaint.setColor(context.getResources().getColor(R.color.color_368FFF));
}
public void setFirstLetters(ArrayList<String> letters) {
firstLetters.clear();
firstLetters.addAll(letters);
invalidate();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec); //獲取寬的模式
int heightMode = MeasureSpec.getMode(heightMeasureSpec); //獲取高的模式
int widthSize = MeasureSpec.getSize(widthMeasureSpec); //獲取寬的尺寸
int heightSize = MeasureSpec.getSize(heightMeasureSpec); //獲取高的尺寸
int width = 0;
int height;
if (widthMode == MeasureSpec.EXACTLY) {
//如果match_parent或者具體的值,直接賦值
width = widthSize;
} else {
//如果其他模式,則指定一個寬度
width = DisplayUtils.dp2px(getContext(), 20.0f);
}
//高度跟寬度處理方式一樣
if (heightMode == MeasureSpec.EXACTLY) {
height = heightSize;
} else {
float textHeight = perTextHeight;
height = (int) (getPaddingTop() + textHeight * (firstLetters.size() + 1) + letterGap * (firstLetters.size() - 1) + getPaddingBottom());
}
if (height > tiku_recycle_answer.getMeasuredHeight()) {
height = tiku_recycle_answer.getMeasuredHeight();
}
//保存測量寬度和測量高度
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < firstLetters.size(); i++) {
paint.setColor(i == choosedPosition ? Color.WHITE : context.getResources().getColor(R.color.color_368FFF));
float x = (getWidth() - paint.measureText(firstLetters.get(i))) / 2;
float y = (float) getHeight() / firstLetters.size();//每個字母的高度
if (i == choosedPosition) {
canvas.drawCircle((float) (getWidth() / 2), i * y + y / 2, bgRadius, backgroundPaint);
}
//垂直位置需要增加一個偏移量,上移兩個像素,因為根據計算得到的是baseline,將字母上移,使其居中在圓內
canvas.drawText(firstLetters.get(i), x, (perTextHeight + y) / 2 + y * i-2, paint);
}
}
//觸碰事件
//按下,松開,拖動
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
this.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
float y = event.getY();
//獲取觸摸到字母的位置
choosedPosition = (int) y * firstLetters.size() / getHeight();
//上滑超過邊界,顯示第一個
if (choosedPosition < 0) {
choosedPosition = 0;
}
//下滑超過邊界,顯示最後一個
if (choosedPosition >= firstLetters.size()) {
choosedPosition = firstLetters.size() - 1;
}
if (listener != null) {
//滑動A-Z字母聯動外層數據
listener.onTouch(firstLetters.get(choosedPosition));
}
break;
case MotionEvent.ACTION_UP:
this.setBackgroundColor(context.getResources().getColor(android.R.color.transparent));
choosedPosition = -1;
if (listener != null) {
//滑動A-Z字母聯動外層數據
listener.onRelease();
}
break;
}
//重繪
invalidate();
return true;
}
public void setFirstListener(OnTouchFirstListener listener) {
this.listener = listener;
}
/**
* OnTouchFirstListener 介面
* onTouch:觸摸到了那個字母
* onRelease:up釋放時中間顯示的字母需要設置為GONE
*/
public interface OnTouchFirstListener {
void onTouch(String firstLetter);
void onRelease();
}
}
<declare-styleable name="SlideBar">
<attr name="letter_size" format="dimension" />
<attr name="letter_height" format="dimension" />
<attr name="letter_gap" format="dimension" />
<attr name="letter_bg_radius" format="dimension" />
</declare-styleable>
xml中引入,我的是constraintlayout,具體設置看自己的布局
<com.answer.view.SlideBar
android:id="@+id/slideBar"
android:layout_width="@dimen/dp_20"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/tiku_recycle_answer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guide_answer"
app:layout_constraintTop_toTopOf="@+id/tiku_recycle_answer"
app:letter_bg_radius="@dimen/dp_8"
app:letter_gap="@dimen/dp_6"
app:letter_height="@dimen/dp_10"
app:letter_size="@dimen/sp_10" />
private void handleSlideBarEvent() {
List<QuesCommentSubjectiveStuBean> datas = .getDatas();//獲取處理後的數據,賦值給導航欄
ArrayList<String> letters = new ArrayList<>();
for (QuesCommentSubjectiveStuBean stuBean : datas) {
if (letters.contains(stuBean.getFirstLetter())) {
continue;
}
letters.add(stuBean.getFirstLetter());
}
slideBar.setFirstLetters(letters);
slideBar.setTiku_recycle_answer(tiku_recycle_answer);
slideBar.setFirstListener(new SlideBar.OnTouchFirstListener() {
@Override
public void onTouch(String firstLetter, float dy) {
tv_first_letter.setVisibility(VISIBLE);
tv_first_letter.setText(firstLetter);
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) tv_first_letter.getLayoutParams();
//如果是第一個字母,修改提示框顯示位置
layoutParams.topMargin = (int) dy + slideBar.getTop() - tv_first_letter.getMeasuredHeight() / 2;
//異常情況,點擊最後一個字元,提示框顯示不全的場景,如果顯示位置超過屏幕,則靠底部顯示
if ((int) dy + slideBar.getTop() + tv_first_letter.getMeasuredHeight() / 2 > tiku_recycle_answer.getBottom()) {
layoutParams.topMargin = tiku_recycle_answer.getBottom() - tv_first_letter.getMeasuredHeight();
}
tv_first_letter.setLayoutParams(layoutParams);
//滑動後移動到對應的位置,找到第一個匹配到首字母的學生,位移到此處
int newPosition = -1;
for (QuesCommentSubjectiveStuBean stuBean : datas) {
if (firstLetter.equals(stuBean.getFirstLetter())) {
newPosition = datas.indexOf(stuBean);
break;
}
}
//move時會多次觸發,此處只響應第一次
if (newPosition != lastPosition) {
lastPosition = newPosition;
Lg.d(TAG, "questionComment-->--滑動導航欄跳轉到首字母:" + firstLetter);
subJectLinearLayoutManager.scrollToPositionWithOffset(lastPosition, 0);
}
}
@Override
public void onRelease() {
postDelayed(new Runnable() {
@Override
public void run() {
lastPosition = -1;
tv_first_letter.setVisibility(GONE);
}
}, 200);
}
});
}
5.一個小問題。
用於放大顯示選中字母的TextView在布局中,請設置為invisible,這樣在載入xml布局時,會對這個控制項進行測量和布局,只是不顯示,這樣我們才能獲得tv_first_letter.getMeasuredHeight(),如果設置為gone,不會進行測量,獲取的高度就為0,這樣在第一次顯示的時候就會有一個顯示位置跳動的異常。設置為invisible就可以解決這個問題,目的就是讓系統測量一下TextView的寬高,不想這么搞的話,在第4步之前手動測量一次也是可以的。
<TextView
android:id="@+id/tv_first_letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp_2"
android:background="@mipmap/ic_bubble"
android:fontFamily="sans-serif"
android:gravity="center"
android:text="C"
android:textColor="@color/color_ffffff"
android:textSize="@dimen/sp_18"
android:visibility="invisible"
app:layout_constraintEnd_toStartOf="@+id/guide_answer"
app:layout_constraintTop_toTopOf="parent" />
③ Android自定義view自定義屬性怎麼使用,請牛人指導
在attrs.xml文件中聲明自定義View的類和相應屬性及其數據類型
<declare-styleablename="ToolBar">
<attrname="icon"format="reference"/>
<attrname="labelColor"format="color"/>
<attrname="isVisible"format="boolean"/>
<attrname="width"format="dimension"/>
<attrname="fromAlpha"format="float"/>
<attrname="buttonNum"format="integer"/>
<attrname="label"format="string"/>
<attrname="pivotX"format="fraction"/>
<attrname="language">
<enumname="english"value="1"/>
<enumname="chinese"value="2"/>
</attr>
<attrname="windowSoftInputMode">
<flagname="stateUnspecified"value="1"/>
<flagname="adjustNothing"value="0x30"/>
</attr>
<attrname="itemBackground"format="reference|color"/>
</declare-styleable>
在自定義View中對自定義屬性進行解析
TypedArrayta=context.obtainStyledAttributes(attrs,R.styleable.ToolBar);
buttonNum=ta.getInt(R.styleable.ToolBar_buttonNum,5);
itemBg=ta.getResourceId(R.styleable.ToolBar_itemBackground,-1);
ta.recycle();
在布局文件中使用自定義屬性,聲明命名空間
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:toolbar="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.demo.ToolBar
toolbar:icon="@drawble/icon"
toolbar:labelColor="#29C1B2"
toolbar:isVisible="true"
toolbar:width="180dp"
toolbar:fromAlpha="0.5"
toolbar:buttonNum="3"
toolbar:label="ToolBar"
toolbar:pivotX="30%"
toolbar:language="english"
toolbar:windowSoftInputMode="stateUnspecified|adjustNothing"
toolbar:itemBackground="@drawable/bg|#00FF00"/>
</LinearLayout>
④ android自定義控制項怎麼用
一、控制項自定義屬性介紹
以下示例中代碼均在values/attrs.xml 中定義,屬性均可隨意命名。
1. reference:參考某一資源ID。
示例:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference" />
<attr name = "src" format = "reference" />
</declare-styleable>
2. color:顏色值。
示例:
<declare-styleable name = "名稱">
<attr name = "textColor" format = "color" />
</declare-styleable>
3. boolean:布爾值。
示例:
<declare-styleable name = "名稱">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
4. dimension:尺寸值。
示例:
<declare-styleable name = "名稱">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
5. float:浮點值。
示例:
<declare-styleable name = "名稱">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
6. integer:整型值。
示例:
<declare-styleable name = "名稱">
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
</declare-styleable>
7. string:字元串。
示例:
<declare-styleable name = "名稱">
<attr name = "text" format = "string" />
</declare-styleable>
8. fraction:百分數。
示例:
<declare-styleable name="名稱">
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
</declare-styleable>
9. enum:枚舉值。
示例:
<declare-styleable name="名稱">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
10. flag:位或運算。
示例:
<declare-styleable name="名稱">
<attr name="windowSoftInputMode">
<flag name = "stateUnspecified" value = "0" />
<flag name = "stateUnchanged" value = "1" />
<flag name = "stateHidden" value = "2" />
<flag name = "stateAlwaysHidden" value = "3" />
</attr>
</declare-styleable>
11.多類型。
示例:
<declare-styleable name = "名稱">
<attr name = "background" format = "reference|color" />
</declare-styleable>
二、屬性的使用以及自定義控制項的實現
1、構思控制項的組成元素,思考所需自定義的屬性。
比如:我要做一個 <帶陰影的按鈕,按鈕正下方有文字說明>(類似9宮格按鈕)
新建values/attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="custom_view">
<attr name="custom_id" format="integer" />
<attr name="src" format="reference" />
<attr name="background" format="reference" />
<attr name="text" format="string" />
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
以上,所定義為custom_view,custom_id為按鈕id,src為按鈕,background為陰影背景,text為按鈕說明,textColor為字體顏色,textSize為字體大小。
2、怎麼自定義控制項呢,怎麼使用這些屬性呢?話不多說請看代碼,CustomView :
package com.nanlus.custom;
import com.nanlus.custom.R;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomView extends FrameLayout implements OnClickListener {
private CustomListener customListener = null;
private Drawable mSrc = null, mBackground = null;
private String mText = "";
private int mTextColor = 0;
private float mTextSize = 20;
private int mCustomId = 0;
private ImageView mBackgroundView = null;
private ImageButton mButtonView = null;
private TextView mTextView = null;
private LayoutParams mParams = null;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.custom_view);
mSrc = a.getDrawable(R.styleable.custom_view_src);
mBackground = a.getDrawable(R.styleable.custom_view_background);
mText = a.getString(R.styleable.custom_view_text);
mTextColor = a.getColor(R.styleable.custom_view_textColor,
Color.WHITE);
mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20);
mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0);
mTextView = new TextView(context);
mTextView.setTextSize(mTextSize);
mTextView.setTextColor(mTextColor);
mTextView.setText(mText);
mTextView.setGravity(Gravity.CENTER);
mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView = new ImageButton(context);
mButtonView.setImageDrawable(mSrc);
mButtonView.setBackgroundDrawable(null);
mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mButtonView.setOnClickListener(this);
mBackgroundView = new ImageView(context);
mBackgroundView.setImageDrawable(mBackground);
mBackgroundView.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addView(mBackgroundView);
addView(mButtonView);
addView(mTextView);
this.setOnClickListener(this);
a.recycle();
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
mParams = (LayoutParams) mButtonView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mButtonView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mBackgroundView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
mBackgroundView.setLayoutParams(mParams);
}
mParams = (LayoutParams) mTextView.getLayoutParams();
if (mParams != null) {
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;
mTextView.setLayoutParams(mParams);
}
}
public void setCustomListener(CustomListener l) {
customListener = l;
}
@Override
public void onClick(View v) {
if (customListener != null) {
customListener.onCuscomClick(v, mCustomId);
}
}
public interface CustomListener {
void onCuscomClick(View v, int custom_id);
}
}
代碼很簡單,就不多說,下面來看看我們的CustomView是怎麼用的,請看:
3、自定義控制項的使用
話不多說,請看代碼,main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal" >
<com.nanlus.custom.CustomView
android:id="@+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
nanlus:background="@drawable/background"
nanlus:custom_id="1"
nanlus:src="@drawable/style_button"
nanlus:text="按鈕1" >
</com.nanlus.custom.CustomView>
</LinearLayout>
</RelativeLayout>
在這里需要解釋一下,
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom"
nanlus為在xml中的前綴,com.nanlus.custom為包名
4、在Activity中,直接上代碼
package com.nanlus.custom;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.nanlus.BaseActivity;
import com.nanlus.custom.R;
import com.nanlus.custom.CustomView.CustomListener;
public class CustomActivity extends BaseActivity implements CustomListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this);
}
@Override
public void onCuscomClick(View v, int custom_id) {
switch (custom_id) {
case 1:
Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
⑤ android 自定義圖片選擇器,怎麼篩選圖片
偽代碼如下:
private boolean flag;
public void onClick(View v){
if(flag){
mImageView.setImageResource(R.drawable.xx1);
}else{
mImageView.setImageResource(R.drawable.xx2);
}
flag = !flag;
}123456789123456789
筆者連上面的代碼知道寫出來那為什麼還要去自定義一個ImageView了?
具體需求:兩個ImageView之間實現單選效果
我們試想下,目前兩個ImageView通過上面的代碼可能還好,只要在不同的事件中做出不同的判斷就好了,但如果一但ImageView增多了了?
A:你不知道用 RadioGroup+RadioButton 啊!
B:是哦!我現在去試下。
……
B:不行啊,雖然RadioButton可以實現,但不好做適配,我為RadioButton設置Drawable,不能居中,而且不能隨著RadioButton的大小改變而改變,資源圖片是多大就多大,顯示區域不夠就不能完全顯示出來。
A:…?,額,是嗎?這樣啊!那我們就自定義一個ImageView來實現吧!
B:為什麼是自定義ImageView?而不是自定義RadioButton?
A:自定義RadioButton實現ImageView的src屬性比較復雜(等著正在看這博客的大神實現),而自定義ImageView來實現單選的屬性比較好實現。
B:那怎麼實現了?
A:看代碼,代碼如下:
attrs.xml <為自定義ImageView添加兩個屬性>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SelectorImageView">
<attr name="selector_src" format="reference"/>//選中的src圖片屬性
<attr name="checked" format="boolean"/>
</declare-styleable>
</resources>12345671234567
Class - SelectorImageView<此類實現了Checkable介面,這里沒什麼特殊功能,而只是利用此介面中的方法而已,不實現我們也可以自己寫>
public class SelectorImageView extends ImageView implements Checkable {
private boolean isChecked;
private Drawable mSelectorDrawable;
private Drawable mDrawable;
public SelectorImageView(Context context) {
this(context, null);
}
public SelectorImageView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SelectorImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
/**獲取默認屬性src的Drawable並用成員變數保存*/
mDrawable = getDrawable();
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SelectorImageView);
/**獲取自定義屬性selector_src的Drawable並用成員變數保存*/
Drawable d = a.getDrawable(R.styleable.SelectorImageView_selector_src);
mSelectorDrawable = d;
/**獲取自定義屬性checked的值並用成員變數保存*/
isChecked = a.getBoolean(R.styleable.SelectorImageView_checked, false);
setChecked(isChecked);
if (d != null && isChecked) {
/**如果在布局中設置了selector_src與checked = true,我們就要設置ImageView的圖片為mSelectorDrawable */
setImageDrawable(d);
}
a.recycle();
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(drawable);
}
@Override
public void setChecked(boolean checked) {
this.isChecked = checked;
}
@Override
public boolean isChecked() {
return isChecked;
}
@Override
public void toggle() {
/**此處依據是否選中來設置不同的圖片*/
if (isChecked()) {
setImageDrawable(mSelectorDrawable);
} else {
setImageDrawable(mDrawable);
}
}
public void toggle(boolean checked){
/**外部通過調用此方法傳入checked參數,然後把值傳入給setChecked()方法改變當前的選中狀態*/
setChecked(checked);
toggle();
}
}
layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.qjay.adf.widget.SelectorImageView
android:id="@+id/iv"
android:layout_width="100dp"
android:layout_height="100dp"
app:selector_src="@mipmap/checked"
android:src="@mipmap/no_checked"/>
</LinearLayout>12345678910111234567891011
Activity Code
public class MainActivity extends Activity {
private SelectorImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (SelectorImageView) findViewById(R.id.iv);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv.toggle(!iv.isChecked());
}
});
}
}