imageView imgFirstAct我想以编程方式设置位图,因为某些原因在运行程序后保持为空。最初,imageView背景和源是空的。
以下是相关的imageViews XMLs:
<ImageView
android:id="@+id/imgFirstAct"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/imgCopyBelow"
android:layout_alignLeft="@+id/imgCaloriesBelow"
android:layout_alignTop="@+id/imgCaloriesBelow"
android:layout_marginRight="89dp"
android:layout_toLeftOf="@+id/imgFifthAct"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:id="@+id/imgCopyBelow"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/imgCaloriesBelow"
android:layout_alignRight="@+id/ImageView03"
android:layout_below="@+id/TextView11"
android:layout_marginTop="15dp"
android:background="@drawable/bar_fill" />
以下是代码:
private ImageView imgFirstAct;
private ImageView imgCopyBelow;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
imgFirstAct = (ImageView)findViewById(R.id.imgFirstAct);
imgCopyBelow = (ImageView)findViewById(R.id.imgCopyBelow);
imgFirstAct.setScaleType(ImageView.ScaleType.FIT_START);
Bitmap bitmap = ((BitmapDrawable)imgCopyBelow.getBackground().getCurrent()).getBitmap();
int h = bitmap.getHeight();
Bitmap croppedBmp = Bitmap.createBitmap(bitmap, 0, 0, 100, h);
imgFirstAct.setImageBitmap(croppedBmp);
}
答案 0 :(得分:2)
mImg.setImageBitmap(img);
设置位图。
mImg
为ImageView
且img
为Bitmap
。如果(2)没有工作,请检查maube,你的位图有问题。
答案 1 :(得分:0)
在findViewById()
之前调用setContentView()@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout); // Call your layout file
imgFirstAct = (ImageView)findViewById(R.id.imgFirstAct);
}
希望它有所帮助(: