解决TextView 中drawableleft 图片大小不可控问题
关键代码
drawable.setBounds(left, top, right, bottom);
textView.setCompoundDrawables(leftDrawable,rightDrawable,topDrawable,bottomDrawable);
使用效果
Screenshot_2017-05-27-14-17-48-229_free.com.timo.png
用法
<?xml version="1.0" encoding="utf-8"?>
<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"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<ImageView
android:id="@+id/iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerVertical="true"
android:src="@mipmap/ic_launcher"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:text="设置"
android:textSize="20dp"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@mipmap/usercenter_grey_arrow"/>
</RelativeLayout>
<free.com.timo.view.widget.TextImageView
android:layout_width="match_parent"
android:layout_height="40dp"
android:drawableLeft="@mipmap/ic_launcher"
android:drawablePadding="20dp"
android:drawableRight="@mipmap/usercenter_grey_arrow"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="设置"
android:textSize="20dp"
app:drawableLeftHeight="30dp"
app:drawableLeftWidth="30dp"
app:drawableRightHeight="20dp"
/>
<free.com.timo.view.widget.TextImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableBottom="@mipmap/ic_launcher"
android:drawableLeft="@mipmap/ic_launcher"
android:drawableRight="@mipmap/ic_launcher"
android:drawableTop="@mipmap/ic_launcher"
android:gravity="center"
android:text="你还可以这样"
app:drawableBottomHeight="100dp"
app:drawableBottomWidth="100dp"
app:drawableLeftHeight="15dp"
app:drawableLeftWidth="15dp"
app:drawableRightHeight="50dp"
app:drawableRightWidth="50dp"
app:drawableTopHeight="30dp"
app:drawableTopWidth="30dp"
/>
</LinearLayout>
属性值设定
<declare-styleable name="TextImageView">
<attr name="drawableLeftWidth" format="dimension"/>
<attr name="drawableLeftHeight" format="dimension"/>
<attr name="drawableTopWidth" format="dimension"/>
<attr name="drawableTopHeight" format="dimension"/>
<attr name="drawableRightWidth" format="dimension"/>
<attr name="drawableRightHeight" format="dimension"/>
<attr name="drawableBottomWidth" format="dimension"/>
<attr name="drawableBottomHeight" format="dimension"/>
</declare-styleable>
自定义view源码
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import free.com.timo.R;
import timber.log.Timber;
/**
* @author Free
* @version 1.0
* @since 2017/5/27
*/
public class TextImageView extends android.support.v7.widget.AppCompatTextView {
private int mLeftWidth;
private int mLeftHeight;
private int mTopWidth;
private int mTopHeight;
private int mRightWidth;
private int mRightHeight;
private int mBottomWidth;
private int mBottomHeight;
public TextImageView(Context context) {
super(context);
}
public TextImageView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public TextImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextImageView);
mLeftWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableLeftWidth, 0);
mLeftHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableLeftHeight, 0);
mTopWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableTopWidth, 0);
mTopHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableTopHeight, 0);
mRightWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableRightWidth, 0);
mRightHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableRightHeight, 0);
mBottomWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableBottomWidth, 0);
mBottomHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableBottomHeight, 0);
typedArray.recycle();
setDrawablesSize();
}
private void setDrawablesSize() {
Drawable[] compoundDrawables = getCompoundDrawables();
Timber.d("Drawable size" + compoundDrawables.length + "value = " + compoundDrawables + "");
for (int i = 0; i < compoundDrawables.length; i++) {
switch (i) {
case 0:
setDrawableBounds(compoundDrawables[0], mLeftWidth, mLeftHeight);
break;
case 1:
setDrawableBounds(compoundDrawables[1], mTopWidth, mTopHeight);
break;
case 2:
setDrawableBounds(compoundDrawables[2], mRightWidth, mRightHeight);
break;
case 3:
setDrawableBounds(compoundDrawables[3], mBottomWidth, mBottomHeight);
break;
default:
break;
}
}
setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], compoundDrawables[2], compoundDrawables[3]);
}
private void setDrawableBounds(Drawable drawable, int width, int height) {
if (drawable != null) {
double scale = ((double) drawable.getIntrinsicHeight()) / ((double) drawable.getIntrinsicWidth());
Timber.d("width/height" + drawable.getIntrinsicWidth() + "," + drawable.getIntrinsicHeight());
Timber.d("scale = %s", scale);
drawable.setBounds(0, 0, width, height);
Rect bounds = drawable.getBounds();
//高宽只给一个值时,自适应
if (bounds.right != 0 || bounds.bottom != 0) {
Timber.d("before" + bounds.right + "," + bounds.bottom);
if (bounds.right == 0) {
bounds.right = (int) (bounds.bottom / scale);
drawable.setBounds(bounds);
}
if (bounds.bottom == 0) {
bounds.bottom = (int) (bounds.right * scale);
drawable.setBounds(bounds);
}
Timber.d("after" + bounds.right + "," + bounds.bottom);
}
}
}
}