이미지 (아이콘)의 크기는 거의 같지만 버튼의 높이를 동일하게 유지하려면 크기를 조정해야합니다.
어떻게해야합니까?
Button button = new Button(this);
button.setText(apiEventObject.getTitle());
button.setOnClickListener(listener);
/*
* set clickable id of button to actual event id
*/
int id = Integer.parseInt(apiEventObject.getId());
button.setId(id);
button.setLayoutParams(new LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT,
android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
Drawable drawable = LoadImageFromWebOperations(apiSizeObject.getSmall());
//?resize drawable here? drawable.setBounds(50, 50, 50, 50);
button.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
답변
이 setBounds()
방법은 모든 유형의 컨테이너에서 작동하지 않습니다 ( ImageView
그러나 일부 컨테이너에서는 작동했습니다 ).
드로어 블 자체의 크기를 조정하려면 아래 방법을 시도하십시오.
// Read your drawable from somewhere
Drawable dr = getResources().getDrawable(R.drawable.somedrawable);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
// Scale it to 50 x 50
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 50, 50, true));
// Set your new, scaled drawable "d"
답변
로 크기 지정 setBounds()
, 즉 50×50 크기 사용
drawable.setBounds(0, 0, 50, 50);
public void setBounds (int left, int top, int right, int bottom)
답변
.setBounds (..)를 적용하기 전에 현재 Drawable을 ScaleDrawable로 변환 해보십시오.
drawable = new ScaleDrawable(drawable, 0, width, height).getDrawable();
그 후
drawable.setBounds(0, 0, width, height);
작동 할 것이다
답변
setBounds () 메서드가 예상대로 비트 맵 드로어 블에서 작동하지 않는 이유를 파헤칠 시간이 없었지만 setBounds가해야 할 일을하기 위해 @ androbean-studio 솔루션을 약간 조정했습니다 …
/**
* Created by ceph3us on 23.05.17.
* file belong to pl.ceph3us.base.android.drawables
* this class wraps drawable and forwards draw canvas
* on it wrapped instance by using its defined bounds
*/
public class WrappedDrawable extends Drawable {
private final Drawable _drawable;
protected Drawable getDrawable() {
return _drawable;
}
public WrappedDrawable(Drawable drawable) {
super();
_drawable = drawable;
}
@Override
public void setBounds(int left, int top, int right, int bottom) {
//update bounds to get correctly
super.setBounds(left, top, right, bottom);
Drawable drawable = getDrawable();
if (drawable != null) {
drawable.setBounds(left, top, right, bottom);
}
}
@Override
public void setAlpha(int alpha) {
Drawable drawable = getDrawable();
if (drawable != null) {
drawable.setAlpha(alpha);
}
}
@Override
public void setColorFilter(ColorFilter colorFilter) {
Drawable drawable = getDrawable();
if (drawable != null) {
drawable.setColorFilter(colorFilter);
}
}
@Override
public int getOpacity() {
Drawable drawable = getDrawable();
return drawable != null
? drawable.getOpacity()
: PixelFormat.UNKNOWN;
}
@Override
public void draw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable != null) {
drawable.draw(canvas);
}
}
@Override
public int getIntrinsicWidth() {
Drawable drawable = getDrawable();
return drawable != null
? drawable.getBounds().width()
: 0;
}
@Override
public int getIntrinsicHeight() {
Drawable drawable = getDrawable();
return drawable != null ?
drawable.getBounds().height()
: 0;
}
}
용법:
// get huge drawable
final Drawable drawable = resources.getDrawable(R.drawable.g_logo);
// create our wrapper
WrappedDrawable wrappedDrawable = new WrappedDrawable(drawable);
// set bounds on wrapper
wrappedDrawable.setBounds(0,0,32,32);
// use wrapped drawable
Button.setCompoundDrawablesWithIntrinsicBounds(wrappedDrawable ,null, null, null);
결과
답변
쓰다
textView.setCompoundDrawablesWithIntrinsicBounds()
minSdkVersion은 build.gradle에서 17이어야합니다.
defaultConfig {
applicationId "com.example..."
minSdkVersion 17
targetSdkVersion 25
versionCode 1
versionName "1.0"
}
드로어 블 크기를 변경하려면 :
TextView v = (TextView)findViewById(email);
Drawable dr = getResources().getDrawable(R.drawable.signup_mail);
Bitmap bitmap = ((BitmapDrawable) dr).getBitmap();
Drawable d = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap, 80, 80, true));
//setCompoundDrawablesWithIntrinsicBounds (image to left, top, right, bottom)
v.setCompoundDrawablesWithIntrinsicBounds(d,null,null,null);
답변
포스트 방법을 사용하여 원하는 효과를 얻으십시오.
{your view}.post(new Runnable()
{
@Override
public void run()
{
Drawable image = context.getResources().getDrawable({drawable image resource id});
image.setBounds(0, 0, {width amount in pixels}, {height amount in pixels});
{your view}.setCompoundDrawables(image, null, null, null);
}
});
답변
아마 조금 늦었을 것입니다. 그러나 여기에 모든 상황에서 마침내 나를 위해 일한 솔루션이 있습니다.
아이디어는 고정 된 크기의 사용자 정의 드로어 블을 만들고 드로잉 작업을 원본 드로어 블에 전달하는 것입니다.
Drawable icon = new ColorDrawable(){
Drawable iconOrig = resolveInfo.loadIcon(packageManager);
@Override
public void setBounds(int left, int top, int right, int bottom){
super.setBounds(left, top, right, bottom);//This is needed so that getBounds on this class would work correctly.
iconOrig.setBounds(left, top, right, bottom);
}
@Override
public void draw(Canvas canvas){
iconOrig.draw(canvas);
}
@Override
public int getIntrinsicWidth(){
return mPlatform.dp2px(30);
}
@Override
public int getIntrinsicHeight(){
return mPlatform.dp2px(30);
}
};