【发布时间】:2014-09-21 05:17:49
【问题描述】:
更新
我正在尝试为一个拥有 50 张壁纸的壁纸应用程序有效地显示位图。
我使用的图像每个 400kb @ 1080x1920
我已经研究并阅读了我应该在应用加载时将每个壁纸的缩小版本加载到内存中,而不是在按下每个壁纸时加载它。
我读过这个http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#read-bitmap
如何使用我当前的代码实现这一点?我从 YouTube 上的本教程获得了这里 http://www.youtube.com/watch?v=SxGYGYBFrOM
这是更新后的代码,我包含了整个 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 已经标记了这些错误。我把它们加粗了。
1)
public static Bitmap decodeSampledBitmapFromResource(**Resources** res, int resId,
int reqWidth, int reqHeight) {
Eclipse 解释:说资源无法解析为类型
2)
int resId = getResources().**getIdentifier**(R.drawable.one, "drawable", getPackageName());
Bitmap whatever = **decodeSampledBitmapFromResource**(getResources(), R.id.IVimage1, 1080, 1500);
Eclipse 解释:
方法 getIdentifier 不适用于参数列表(对不起,如果我简单地错误地引用了可绘制对象)
MainActivity 类型的方法 decodeSampledBitmapFromResource 引用缺失的类型资源。
非常感谢您迄今为止的帮助,能得到如此快速的回复真是太好了。
编辑
资源名称:多个资源,命名为一、二、三、四、五,均带有 jpg 扩展名。
资源位置: res/drawable-hdpi/one.jpg res/drawable-hdpi/two.jpg .. res/drawable-hdpi/two.jpg
【问题讨论】:
标签: android eclipse caching memory bitmap