Android的scaledrawable似乎并不工作

Android的scaledrawable似乎并不工作

本文介绍了Android的scaledrawable似乎并不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想出来的机器人ScaleDrawable

但它似乎没有工作......我创建的XML作为文档的例子,我在绘制文件夹logo.png

But it doesn't seem to work... I created the xml as in the documentation example and I have a logo.png in the drawable folder

< XML版本=1.0编码=UTF-8&GT?;    <规模以上的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android        机器人:可绘制=@可绘制/标志        机器人:scaleGravity =center_vertical | center_horizo​​ntal        机器人:scaleHeight =80%        机器人:scaleWidth =80%/>

<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/logo" android:scaleGravity="center_vertical|center_horizontal" android:scaleHeight="80%" android:scaleWidth="80%" />

我救了这是logo2.xml在绘制文件夹,然后我有一个ImageView的一个LinearLayout中,并设置成@绘制/ LOGO2,但标志不会出现的ImageView的src ......我做得错在这里?你如何实际使用的规模元素?

I saved this as logo2.xml in the drawable folder then I have a LinearLayout with an ImageView and set the src of the ImageView to "@drawable/logo2" but the logo doesn't appear...Am I doing something wrong here? How do you actually use the scale element?

谢谢

斯蒂芬

推荐答案

就在今天,我面临同样的问题。该ScaleDrawable似乎并没有工作。经过一番调查,我已经找到了解决办法:

Just today I faced the same problem. The ScaleDrawable does not seem to work. After some investigation I've found the solution:

如果你看一下documentation你可以找到这句话:一个绘制对象改变另一个可绘制基于其当前电平值的大小就是这样。

If you look at the documentation you can find this phrase: "A Drawable that changes the size of another Drawable based on its current level value." That's it.

很明显后,我发现了<一href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;f=graphics/java/android/graphics/drawable/ScaleDrawable.java;h=a95eb06e49d74ab456df56ce47732311b84e37fa;hb=HEAD">onBoundsChange()函数使用的怪等级变量。

It became clear after I found the onBoundsChange() function uses the strange level variable.

有关您的code将是这样的:

For you the code would be something like this:

Resources res = getResources();
ScaleDrawable sd = (ScaleDrawable) res.getDrawable(R.drawable.logo2);
Drawable d = sd.getDrawable();

d.setLevel(1);

ImageView iv = new ImageView(this);
iv.setImageDrawable(sd);
iv.setAdjustViewBounds(true);
iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

setContentView(iv);

更新的:我想在Draw()函数的问题。由于初始化后级变量0:

Update: I think the problem in the draw() function. Because after an initialization the level variable is 0:

public void draw(Canvas canvas) {
    if (mScaleState.mDrawable.getLevel() != 0)
        mScaleState.mDrawable.draw(canvas);
}

更新的:也许那么这个code可以帮助你:

Update: Maybe then this code may help you:

private void useScaledImage() {
    Resources res = getResources();
    BitmapDrawable bd = (BitmapDrawable)res.getDrawable(R.drawable.sun);
    Bitmap b = Bitmap.createScaledBitmap(bd.getBitmap(),
                         (int) (bd.getIntrinsicHeight() * 0.7),
                         (int) (bd.getIntrinsicWidth() * 0.7),
                         false);

    LinearLayout l = new LinearLayout(this);
    ImageView iv = new ImageView(this);

    iv.setImageDrawable(new BitmapDrawable(b));
    iv.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    l.addView(iv);

    setContentView(l);
}

这篇关于Android的scaledrawable似乎并不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:03