根据屏幕大小缩放ImageButton

时间:2014-05-05 06:56:49

标签: java android image-scaling autoscaling android-imagebutton

在我的应用程序中,我可以使用此类

缩放ImageView

`public class resizeAvatar扩展了View {

private final Drawable sfondoAvatar;

public resizeAvatar(Context context) {
    super(context);

    sfondoAvatar = context.getResources().getDrawable(R.drawable.main_voice);
    setBackgroundDrawable(sfondoAvatar);

}

public resizeAvatar(Context context, AttributeSet attrs) {
    super(context, attrs);

    sfondoAvatar = context.getResources().getDrawable(R.drawable.main_voice);
    setBackgroundDrawable(sfondoAvatar);
}

public resizeAvatar(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);

    sfondoAvatar = context.getResources().getDrawable(R.drawable.main_voice);
    setBackgroundDrawable(sfondoAvatar);
}

@Override
protected void onMeasure(int widthMeasureSpec,
                                   int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = width * sfondoAvatar.getIntrinsicHeight() / sfondoAvatar.getIntrinsicWidth();
    setMeasuredDimension(width, height);
}

} `

写在布局中:

<com.myapp.app.resizeAvatar android:id="@+id/mainvoice" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_centerVertical="true"/>

但是通过这种方式我不能拥有onClick事件..我需要做同样但使用ImageButton ..有办法吗?

1 个答案:

答案 0 :(得分:1)

在包含此视图的活动中,在onCreate方法中执行以下操作:

resizeAvatar myMainvoice  = (resizeAvatar) v.findViewById(R.id.mainvoice);
myMainVoice.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            { 
               //place your on click logic here 
            }
        });