Neatname
Neatname

Reputation: 134

Displaying a Bitmap in Android

I cannot for the life of me figure out how to display a Bitmap in Android. It seems like you just have to call View.setImageBitmap() and pass it your bitmap, but nothing appears on the screen when I do this. Nothing happens if I call View.invalidate() either. Can anybody point me in the right direction? Here's what I have for the generation of Bitmap.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    ImageView myView = (ImageView) findViewById(R.id.imageView);
    Bitmap test = Bitmap.createBitmap(800, 800, Bitmap.Config.ARGB_8888);
    test.eraseColor(Color.argb(255, 255, 0, 0));
    myView.setImageBitmap(test);
}

And here's what the content_*.xml looks like.

<?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: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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="MyActivity"
    tools:showIn="@layout/activity_generator"
    android:visibility="visible">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:visibility="visible" />
</RelativeLayout>

Upvotes: 0

Views: 1004

Answers (1)

One way to achieve this, firstly your class must extends View (and not Activity).

Then you implement the obligatory methods:

public myClassName(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(); }

public myClassName(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(); }

public myClassName(Context context) {
    super(context);
    init(); }

Then you define your init():

private void init() {
    Bitmap original = BitmapFactory.decodeResource(getResources(),
            R.drawable.myVeryOwnChoosedBitmap); //inside your drawable
    mBitmap = Bitmap.createBitmap(256, 256, Bitmap.Config.ARGB_8888);
    mCanvas = new Canvas(mBitmap);
    mCanvas.drawColor(0xff808080); // bgcolor
    mPaint = new Paint();
}

Override onDraw():

@Override
protected void onDraw(Canvas canvas) {
    drawBMP(canvas);
    // In 20ms will be redrawn
    postInvalidateDelayed(20);
}

Then drawBMP:

private void drawBMP(Canvas canvas) {
    mPaint.setFilterBitmap(false);
    canvas.drawBitmap(original, 0, 0, mPaint);
}

Upvotes: 1

Related Questions