Skip to content

Instantly share code, notes, and snippets.

@xxnjdlys
Created October 14, 2015 05:10
Show Gist options
  • Save xxnjdlys/c1717846852d14ec7b68 to your computer and use it in GitHub Desktop.
Save xxnjdlys/c1717846852d14ec7b68 to your computer and use it in GitHub Desktop.
可以设置textview图片的大小
package com.wukongtv.wkremote.client.widget;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;
import com.wukongtv.wkremote.client.R;
/**
* Created by sadieyu
* Date: 15-10-14.
* Time: ${Time}.
* Pkg: com.wukongtv.wkremote.client.widget
* <p/>
* resource in attrs.xml
* <declare-styleable name="TextViewDrawableSize">
* <attr name="compoundDrawableWidth" format="dimension" />
* <attr name="compoundDrawableHeight" format="dimension" />
* </declare-styleable>
*
* <com.wukongtv.wkremote.client.widget.TextViewDrawableSize
* android:layout_width="wrap_content"
* android:layout_height="wrap_content"
* android:drawableLeft="@drawable/ic_launcher"
* android:drawablePadding="4dp"
* app:compoundDrawableHeight="16dp"
* app:compoundDrawableWidth="16dp"/>
*/
public class TextViewDrawableSize extends TextView {
private int mDrawableWidth;
private int mDrawableHeight;
public TextViewDrawableSize(Context context) {
this(context, null);
}
public TextViewDrawableSize(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public TextViewDrawableSize(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr, 0);
}
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextViewDrawableSize, defStyleAttr, defStyleRes);
try {
mDrawableWidth = array.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableWidth, -1);
mDrawableHeight = array.getDimensionPixelSize(R.styleable.TextViewDrawableSize_compoundDrawableHeight, -1);
} finally {
array.recycle();
}
initCompoundDrawableSize();
}
private void initCompoundDrawableSize() {
if (mDrawableWidth > 0 || mDrawableHeight > 0) {
Drawable[] drawables = getCompoundDrawables();
for (Drawable drawable : drawables) {
if (drawable == null) {
continue;
}
Rect realBounds = drawable.getBounds();
float scaleFactor = realBounds.height() / (float) realBounds.width();
float drawableWidth = realBounds.width();
float drawableHeight = realBounds.height();
if (mDrawableWidth > 0) {
// save scale factor of image
if (drawableWidth > mDrawableWidth) {
drawableWidth = mDrawableWidth;
drawableHeight = drawableWidth * scaleFactor;
}
}
if (mDrawableHeight > 0) {
// save scale factor of image
if (drawableHeight > mDrawableHeight) {
drawableHeight = mDrawableHeight;
drawableWidth = drawableHeight / scaleFactor;
}
}
realBounds.right = realBounds.left + Math.round(drawableWidth);
realBounds.bottom = realBounds.top + Math.round(drawableHeight);
drawable.setBounds(realBounds);
}
setCompoundDrawables(drawables[0], drawables[1], drawables[2], drawables[3]);
}
}
}
@xxnjdlys
Copy link
Author

    <com.wukongtv.wkremote.client.widget.TextViewDrawableSize
        android:id="@+id/tv_sourcelist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@drawable/video_detail_sourcelist_selector"
        android:gravity="center"
        android:padding="4dp"
        android:drawableLeft="@drawable/ic_launcher"
        android:drawablePadding="4dp"
        app:compoundDrawableHeight="16dp"
        app:compoundDrawableWidth="16dp"
        android:textColor="@color/video_detail_80_white"
        android:layout_marginRight="12dp"/>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment