Android 图像未在imageview onelplus、棒棒糖中设置
我试图创建一个,它在我测试的所有手机上都能工作,除了oneplus设备,在oneplus 1和oneplus X中,图像没有设置,图像视图是透明的。我尝试通过编程和xml来设置图像。在android版棒棒糖的这些设备中使用高分辨率图像有什么问题吗 我的活动:Android 图像未在imageview onelplus、棒棒糖中设置,android,android-imageview,android-5.0-lollipop,oneplusone,Android,Android Imageview,Android 5.0 Lollipop,Oneplusone,我试图创建一个,它在我测试的所有手机上都能工作,除了oneplus设备,在oneplus 1和oneplus X中,图像没有设置,图像视图是透明的。我尝试通过编程和xml来设置图像。在android版棒棒糖的这些设备中使用高分辨率图像有什么问题吗 我的活动: int ht,wi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIn
int ht,wi;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
wi = width *2/3;
ht = height * 3/5;
ImageView wireframe = (ImageView) findViewById(R.id.wireframe);
BitmapWorkerTask task = new BitmapWorkerTask(wireframe);
task.execute(R.drawable.wireframe);
}
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);
}
class BitmapWorkerTask extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private int data = 0;
public BitmapWorkerTask(ImageView imageView) {
// Use a WeakReference to ensure the ImageView can be garbage collected
imageViewReference = new WeakReference<ImageView>(imageView);
}
// Decode image in background.
@Override
protected Bitmap doInBackground(Integer... params) {
data = params[0];
Bitmap bitmap = decodeSampledBitmapFromResource(getResources(), data, wi, ht);
return bitmap;
}
// Once complete, see if ImageView is still around and set bitmap.
@Override
protected void onPostExecute(Bitmap bitmap) {
if (imageViewReference != null && bitmap != null) {
final ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
int-ht,wi;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_教程);
Display Display=getWindowManager().getDefaultDisplay();
点大小=新点();
display.getSize(size);
int width=size.x;
整数高度=大小y;
wi=宽度*2/3;
ht=高度*3/5;
ImageView线框=(ImageView)findViewById(R.id.wireframe);
BitmapWorkerTask任务=新的BitmapWorkerTask(线框);
执行(R.drawable.wireframe);
}
公共静态整数计算示例大小(
BitmapFactory.Options选项、int reqWidth、int reqHeight){
//图像的原始高度和宽度
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>要求高度| |宽度>要求宽度){
最终int半高=高度/2;
最终整数半宽度=宽度/2;
//计算最大的inSampleSize值,该值为2的幂次方,并同时保持这两个值
//高度和宽度大于请求的高度和宽度。
而((半高/采样)大于所需高度
&&(半宽/采样尺寸)>reqWidth){
inSampleSize*=2;
}
}
返回样本大小;
}
公共静态位图解码SampledBitMapFromResource,
输入要求宽度,输入要求高度){
//使用INJUSTDECBOUNDS首次解码=true检查尺寸
final BitmapFactory.Options=new BitmapFactory.Options();
options.inJustDecodeBounds=true;
解码资源(res、resId、options);
//计算样本大小
options.inSampleSize=计算样本大小(options、reqWidth、reqHeight);
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=false;
返回BitmapFactory.decodeResource(res、resId、options);
}
类BitmapWorkerTask扩展了AsyncTask{
私有最终WeakReference imageViewReference;
私有整数数据=0;
公共位图工作任务(ImageView ImageView){
//使用WeakReference确保可以对ImageView进行垃圾收集
imageViewReference=新的WeakReference(imageView);
}
//在背景中解码图像。
@凌驾
受保护位图doInBackground(整数…参数){
数据=参数[0];
位图位图=decodeSampledBitmapFromResource(getResources(),数据,wi,ht);
返回位图;
}
//完成后,查看ImageView是否仍然存在并设置位图。
@凌驾
受保护的void onPostExecute(位图){
if(imageViewReference!=null&&bitmap!=null){
最终ImageView=imageViewReference.get();
如果(imageView!=null){
设置图像位图(位图);
}
}
}
}
我的xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:descendantFocusability="afterDescendants">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="30dp">
<ImageView
android:id="@+id/wireframe"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/wireframe"/>
</RelativeLayout>
<TextView
android:id="@+id/tutorial_skip"
android:onClick="onLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SKIP LOGIN"
android:padding="5dp"
android:layout_gravity="right"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:textColor="#ffffff"
android:textSize="15dp"
android:textStyle="bold"/>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_weight="3"
android:id="@+id/bottom_layout"
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
</LinearLayout>
</LinearLayout>
我尝试加载的图像:
我使用了分辨率更低的newimage,现在在imageview中显示图像
我使用分辨率低得多的newimage,现在在imageview中显示图像
你可以共享你的代码吗?你可以使用图像加载库,例如,它可以确保在所有类型的设备上加载高分辨率图像。你可以共享你的代码吗?你可以使用图像加载库,例如,它可以确保在所有类型的设备上加载高分辨率图像。