我试图编写我的第一个应用程序并且遇到使用图像的问题。我能够在应用程序中显示图像,但由于其高分辨率,加载时间很慢。我去添加一个位图并从developers.android.com找到示例代码,但是当我实现它时,图像不再被看到。由于直接位于其下方的文本位于正确的位置,因此图像视图仍然是我所知道的。
我还说我也不确定在哪里正确实现位图代码,它可能在错误的类文件中。
显示图片的活动:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_ion"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.michael.titanfall2apptest.Ion">
<ImageView
app:srcCompat="@drawable/ion"
android:id="@+id/ionimageView"
android:layout_centerHorizontal="true"
android:layout_width="175dp"
android:layout_height="175dp"/>
<TextView
android:text="Ion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ionimageView"
android:layout_centerHorizontal="true"
android:id="@+id/textView"/>
</RelativeLayout>
活动类:
public class Ion extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ion);
ImageView ionImageView = (ImageView) findViewById(R.id.ionimageView);
ionImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.ionimageView, 500, 500));
}
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;
}
}
Log.d("test", "Look here");
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);
}
任何资源建议也将受到赞赏。我还没有找到一个视频来解释这一点。
答案 0 :(得分:0)
这是因为你传递了ImageView的id,它只是image的容器:
ionImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.ionimageView, 500, 500));
您需要传递可绘制图像的ID(而不是ImageView):
ionImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.drawable.ion, 500, 500));
还删除:
app:srcCompat="@drawable/ion"
在您的布局中。