Android的 - 保持纵横比对背景图像上的默认按钮 [英] Android - Keep aspect ratio on background image on default Button

查看:114
本文介绍了Android的 - 保持纵横比对背景图像上的默认按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法保持绘制的长宽比时,作为一个按钮背景适用(不能使用的ImageButton (需要在按钮文字可能性),不能使用 setCompoundDrawables(),因为那也没有满足我的要求(我做我自己的类来支持添加背景,同时仍保留了原生Android默认的按钮,再加上我需要设置按钮的中间德的背景下,不左,右,顶部或底部))。

Is there any way to maintain the aspect ratio of a drawable when applied as background on a Button (cannot use ImageButton (need possibility for text on the Button), cannot use setCompoundDrawables(), because that also doesn't meet my requirements (I made my own class to support adding backgrounds while still retaining the original android default button, plus I need to set te background in the middle of the button, not left, right, top or bottom)).

我试着用溶液中的 ScaleDrawable ,但无论哪种,我做错了什么,或不工作:

I tried a solution with a ScaleDrawable, but either I'm doing something wrong or it doesn't work:

Bitmap scaled = Bitmap.createScaledBitmap(original, (int)newBmpWidth, (int)newBmpHeight, false);
BitmapDrawable bd = new BitmapDrawable(scaled);
ScaleDrawable sd = new ScaleDrawable(bd, Gravity.CENTER, a, b);
//for a and b I tried lots of different values, ranging from 0.07f to 1000
sd.setLevel(1);

然后我使用 ScaleDrawable 为背景,但它无论如何得到拉伸。

Then I use the ScaleDrawable as background, but it get stretched anyway.

我也尝试了 InsetDrawable ,但同样,不是我做错了什么,或不工作,预计第一:

I also tried a InsetDrawable, but again, either I'm doing something wrong or it doesn't work, expecting the first:

BitmapDrawable mBitmapDrawable = new BitmapDrawable(mBitmap);
InsetDrawable mInsetDrawable = new InsetDrawable(mBitmapDrawable, mLeftOffset, 0, mRightOffset, 0);

然后我使用 InsetDrawable 作为背景,但是这也得到反正捉襟见肘。

Then I use the InsetDrawable as background, but this also get stretched anyway.

有什么办法来应用背景,但保持在按钮宽高比我可以找到很多答案为 ImageButtons ,但可悲的是一个按钮不具有 setImageBitmap() setScaleType()方法。

Is there any way to apply a background but maintain aspect ratio on a Button? I can find plenty answers for ImageButtons, but sadly a Button does not have a setImageBitmap() or setScaleType() method.

推荐答案

最近,我遇到了同样的问题设置了的ImageView 的背景图像。该解决方案,我想出了用作品的任何查看,所以它应该覆盖按钮以及

I faced the same problem recently for setting the background image of an ImageView. The solution I came up with works with any View, so it should cover Button as well.

问题是,查看一直延伸其背景的查看的宽度和高度的充分程度,不管可绘制的定标为背景提供。当你对不能提供专门的可绘制建议报告,这不能被缩放图像的任何手段将其设置为背景之前,抵消因为查看只会把图像尺寸,不管他们是,再独立地扩展他们填补了查看的区域。

The issue is that View always stretches its background to the full extent of View's width and height, no matter the scaling of the Drawable provided as background. As your report on failing to provide specialized Drawables suggests, this cannot be counteracted by any means of scaling an image prior to setting it as background because View would simply take the image dimensions, whatever they are, and scale them again independently to fill the View's area.

我的解决方案利用基于这样的事实,如果我们能为用户提供相同的纵横比的查看的背景图像,将不会有失真的不同的方法。

My solution takes a different approach based on the fact that if we can provide a background image with same aspect ratio as the View's, there would be no distortion.

功能 scaleKeepingAspect()下面需要的图像资源,并回答了 BitmapDrawable 包含缩放到原始图像所需的宽度和高度。缩放是在原始图像的纵横比被保持的方式执行,并最终剩余空间,以适合所需的箱填充有透明的像素。原始图像为中心的结果形象。

Function scaleKeepingAspect() below takes an image resource and answers a BitmapDrawable that contains the original image scaled to a desired width and height. The scaling is performed in a way that the aspect ratio of the original image is kept, and eventual remaining spaces to fit the desired box are filled with transparent pixels. The original image is centered in the result image.

private BitmapDrawable scaleKeepingAspect(Resources res, int id, int dstWidth, int dstHeight) {

    Bitmap b = (new BitmapFactory()).decodeResource(res, id);
    float scaleX = (float) dstWidth / b.getWidth();
    float scaleY = (float) dstHeight / b.getHeight();
    float scale = scaleX < scaleY ? scaleX : scaleY;
    int sclWidth = Math.round(scale * b.getWidth());
    int sclHeight = Math.round(scale * b.getHeight());
    b = Bitmap.createScaledBitmap(b, sclWidth, sclHeight, false);
    int[] pixels = new int[sclWidth * sclHeight];
    b.getPixels(pixels, 0, sclWidth, 0, 0, sclWidth, sclHeight);
    b = Bitmap.createBitmap(dstWidth, dstHeight, b.getConfig());
    b.setPixels(pixels, 0, sclWidth, (dstWidth - sclWidth) / 2, (dstHeight - sclHeight) / 2, sclWidth, sclHeight);
    return new BitmapDrawable(res, Bitmap.createBitmap(b));

}

一个适当的地方使用 scaleKeepingAspect()中设置的背景查看是布局更改时事件,所以后台更新正确时视图的边界由于布局处理的变化。要做到这一点,假设(一)你的查看 MyView的在布局XML标识,并且(b)您背景图像文件 RES /绘制/ background.png ,请执行以下操作:

A proper place to make use of scaleKeepingAspect() in setting the background of a View is during layout change events, so the background is updated properly when the bounds of the View change due to layout processing. To do so, assuming (a) your View is identified by myView in your layout xml, and (b) your background image is file res/drawable/background.png, do the following:


  1. 声明你的活动作为实现者 View.OnLayoutChangeListener 接口:

public class MyActivity extends Activity implements View.OnLayoutChangeListener {

    ...

}


  • 填写您的活动作为侦听器布局的变化你的查看

  • Register your Activity as a listener to layout changes of your View:

    @Override public void onCreate(Bundle savedInstanceState) {
    
        ...
    
        myView = (View) findViewById(R.id.myView);
        myView.addOnLayoutChangeListener(this);
    
        ...
    
    }
    


  • 对于检测查看布局更改处理布局更新,并在必要时更新其背景的:

  • Detect for View layout updates in the layout change handler, and update its background when needed:

    @Override public void onLayoutChange (View v,
            int left, int top, int right, int bottom,
            int oldLeft, int oldTop, int oldRight, int oldBottom) {
    
        if (left == right || top == bottom || (
                left == oldLeft && top == oldTop &&
                right == oldRight && bottom == oldBottom))
            return;
    
        switch (v.getId()) {
    
            case R.id.myView:
    
                v.setBackground(
                    scaleKeepingAspect(
                        getResources(),
                        R.drawable.background,
                        v.getWidth(),
                        v.getHeight()));
                break;
    
        }
    
    }
    


  • 这篇关于Android的 - 保持纵横比对背景图像上的默认按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆