【问题标题】:Scaling ImageButton basing on screen size根据屏幕大小缩放图像按钮
【发布时间】:2014-06-21 09:47:33
【问题描述】:

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

`public class resizeAvatar extends 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.. 有没有办法?

【问题讨论】:

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


    【解决方案1】:

    在包含此视图的 Activity 和 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 
                }
            });
    

    【讨论】:

    • 是的,它有效!我没有考虑过这个解决方案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2023-04-03
    • 2017-12-02
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2022-10-17
    • 1970-01-01
    相关资源
    最近更新 更多