简体   繁体   English

根据屏幕尺寸缩放ImageButton

[英]Scaling ImageButton basing on screen size

In my application i can scaling an ImageView using this class 在我的应用程序中,我可以使用此类缩放ImageView

`public class resizeAvatar extends View { 公共类resizeAvatar扩展了视图{

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);
}

} ` }`

And writing in the layout: 并在布局中编写:

<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"/>

But in this way i can't have the onClick event.. i need do the same but using an ImageButton.. Is there a way? 但是以这种方式,我无法拥有onClick事件..我需要做同样的事情,但使用ImageButton ..有办法吗?

In the activity where you include this view and in the onCreate method do the following: 在包含此视图的活动中,并在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 
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM