Skip to content

Instantly share code, notes, and snippets.

@zyzo zyzo/android-bitmap
Created Mar 24, 2014

Embed
What would you like to do?
Android snippet - displaying and playing around with Bitmap image
package com.dha.contagion;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
public class Game extends Activity {
Bitmap mCurrentBitmap;
private int i;
// to reuse one bitmap
BitmapFactory.Options mBitmapOptions;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
mBitmapOptions = new BitmapFactory.Options();
mBitmapOptions.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.pion_bleu_outlined_black, mBitmapOptions);
mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth,
mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
mBitmapOptions.inJustDecodeBounds = false;
mBitmapOptions.inBitmap = mCurrentBitmap;
mBitmapOptions.inSampleSize = 1;
BitmapFactory.decodeResource(getResources(), R.drawable.pion_rouge_outlined_black, mBitmapOptions);
imageView.setImageBitmap(mCurrentBitmap);
final float memoX = imageView.getX();
final float memoY = imageView.getY();
Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (imageView.getX() >= size.x || imageView.getY() >= size.y) {
imageView.setX(memoX); imageView.setY(memoY);
}
imageView.setX(imageView.getX()+i);
imageView.setY(imageView.getY()+i);
imageView.setRotation(imageView.getRotation()+i);
i++;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.game, menu);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.