Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓系统-试图为目前尺寸为0.5MB的壁纸应用程序高效地显示位图_Android_Eclipse_Caching_Memory_Bitmap - Fatal编程技术网

Android 安卓系统-试图为目前尺寸为0.5MB的壁纸应用程序高效地显示位图

Android 安卓系统-试图为目前尺寸为0.5MB的壁纸应用程序高效地显示位图,android,eclipse,caching,memory,bitmap,Android,Eclipse,Caching,Memory,Bitmap,更新 我正在尝试为一个能容纳50张壁纸的壁纸应用程序高效地显示位图 我使用的图像都是400kb@1080x1920 我已经研究并阅读了我应该在应用程序加载时将每张壁纸的缩小版本加载到内存中,而不是在按下时加载每张壁纸 我读过这个 我将如何使用当前代码实现这一点?我是从YouTube上的这个教程中得到的 这是更新的代码,我包括了我的整个MainActivity类 package com.example.ultimateabstractwallpaperhd; import java.io.IOE

更新

我正在尝试为一个能容纳50张壁纸的壁纸应用程序高效地显示位图

我使用的图像都是400kb@1080x1920

我已经研究并阅读了我应该在应用程序加载时将每张壁纸的缩小版本加载到内存中,而不是在按下时加载每张壁纸

我读过这个

我将如何使用当前代码实现这一点?我是从YouTube上的这个教程中得到的

这是更新的代码,我包括了我的整个MainActivity类

package com.example.ultimateabstractwallpaperhd;

import java.io.IOException;

public class MainActivity extends Activity implements OnClickListener {
// Two classes you mentioned I put here.

ImageView display;
int toPhone;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    // check if next two lines are necessary
    requestWindowFeature(Window.FEATURE_NO_TITLE); // gets rid of app title
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,       WindowManager.LayoutParams.FLAG_FULLSCREEN);

    if (android.os.Build.VERSION.SDK_INT>=11) {
           getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
        }

    setContentView(R.layout.wallpaper);
    toPhone = R.drawable.one;

display = (ImageView) findViewById(R.id.IVdisplay);
ImageView one = (ImageView) findViewById(R.id.IVimage1);
ImageView two = (ImageView) findViewById(R.id.IVimage2);
ImageView three = (ImageView) findViewById(R.id.IVimage3);
ImageView four = (ImageView) findViewById(R.id.IVimage4);
ImageView five = (ImageView) findViewById(R.id.IVimage5);
Button setWall = (Button) findViewById(R.id.BsetWallpaper);


one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
four.setOnClickListener(this);
five.setOnClickListener(this);
setWall.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()){
    case R.id.IVimage1:
        display.setImageResource(R.drawable.one);
        toPhone = R.drawable.one;
        break;
    case R.id.IVimage2:
        display.setImageResource(R.drawable.two);
        toPhone = R.drawable.two;
        break;
    case R.id.IVimage3:
        display.setImageResource(R.drawable.three);
        toPhone = R.drawable.three;
        break;
    case R.id.IVimage4:
        display.setImageResource(R.drawable.four);
        toPhone = R.drawable.four;
        break;
    case R.id.IVimage5:
        display.setImageResource(R.drawable.five);
        toPhone = R.drawable.five;
        break;

    case R.id.BsetWallpaper:
        InputStream yeaaaa  = getResources().openRawResource(toPhone);

        Bitmap whatever = decodeSampledBitmapFromResource(getResources(), R.id.IVimage1, 1080, 1500)

        try {
            getApplicationContext().setWallpaper(whatever);
        }catch(IOException e){
            e.printStackTrace();
        }
        break;

    }
}
}

编辑:

好的,我的资源在drawable hpdi文件夹中

我仍然有一些错误,eclipse已经标记了这些错误。我用黑体字写的

一,

Eclipse解释:表示无法将资源解析为类型

二,

日食解释:

方法getIdentifier不适用于参数列表抱歉,如果我简单地错误引用了drawable

类型MainActivity中的decodeSampledBitmapFromResource方法引用缺少的类型资源

非常感谢你迄今为止的帮助,能得到如此迅速的回复真是太好了

编辑

资源名称:多个资源,分别命名为1、2、3、4、5,所有资源都具有jpg扩展名

资源的位置: res/drawable hdpi/one.jpg res/drawable hdpi/two.jpg ..
res/drawable hdpi/two.jpg

因此,如果查看您提到的文档,您希望在类中创建以下两个方法:

   public static int calculateInSampleSize(
                BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > reqHeight
                    && (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}
而不是:

Bitmap whatever = BitmapFactory.decodeStream(yeaaaa);
使用

不要将资源放入原始文件夹,将其放入drawables文件夹,以便使用资源ID

您可以这样获得resourceID:

getResources().getIdentifier([what ever your named your drawable] , "drawable", getPackageName());
编辑

根据您的ajusted代码,有几件事需要注意

看起来您传递的是ImageView,而不是位图的实际资源

这一行:

 Bitmap whatever = decodeSampledBitmapFromResource(getResources(), R.id.IVimage1, 1080, 1500)
应该是:

int resId = getResources().getIdentifier([what ever your named your drawable] , "drawable", getPackageName());

 Bitmap whatever = decodeSampledBitmapFromResource(getResources(), resId, 1080, 1500)
备选答复:

InputStream yeaaaa  = getResources().openRawResource(toPhone)
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; \\1 would be the full resolution, 2 is half the size, 4 a quarter etc..
Bitmap whatever = BitmapFactory.decodeStream(inputStream, null, options);

啊,好的,现在的问题是因为每次按下图像时,resourceID下面的代码都会更改。我在回答中添加了一行代码,说明如何从可绘图文件夹中获取资源id。好的,现在的问题是因为每次按下图像时,resourceID下面的代码都会更改。这是我用来改变它的变量。此外,我还得到一个错误,即资源无法解析为类型。谢谢你的快速回复!!非常感谢。你能更新你的代码吗?这样我就可以更好地理解你的意思了。添加了你的建议,但仍然不能正常工作。啊!它工作得很好。我不想说,但我没有问正确的问题。目前,您给我的帮助非常好,这意味着在设置壁纸后,它将以原始分辨率的1/x*显示。我应该问的是如何在应用程序中以较低的分辨率显示图像,以及如何高效地做到这一点,比如在启动时加载所有图像,这样在使用应用程序时就不会出现延迟。我很抱歉在这方面没有直截了当。如果您有任何想法,我们将不胜感激。??我该怎么办??应该怎么做?你应该考虑将图像解码移动到一个异步任务:帮助释放应用程序的UI。上面的答案告诉你如何减少取样。。采样。请投票表决我的答案,并接受一个作为答案。我为rep points工作:
 Bitmap whatever = decodeSampledBitmapFromResource(getResources(), R.id.IVimage1, 1080, 1500)
int resId = getResources().getIdentifier([what ever your named your drawable] , "drawable", getPackageName());

 Bitmap whatever = decodeSampledBitmapFromResource(getResources(), resId, 1080, 1500)
InputStream yeaaaa  = getResources().openRawResource(toPhone)
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2; \\1 would be the full resolution, 2 is half the size, 4 a quarter etc..
Bitmap whatever = BitmapFactory.decodeStream(inputStream, null, options);