imageView imgFirstAct 我想以编程方式设置位图,由于某种原因在运行程序后保持为空。最初 imageView 背景和源是空的。
这是相关的 imageViews XML:
<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);
}