Skip to content

Instantly share code, notes, and snippets.

@alexfu
Created February 4, 2014 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexfu/8797814 to your computer and use it in GitHub Desktop.
Save alexfu/8797814 to your computer and use it in GitHub Desktop.
Prevent your BitmapDrawables from scaling when using setBackground/setBackgroundDrawable. This is usually desired when you want to set an image as your background.
public class DontScaleMe extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.alayout);
ViewGroup parent = findViewById(R.id.root);
Bitmap bitmap = obtainBitmapFromSomewhere();
// This will scale your Bitmap to fit it's bounds.
parent.setBackground(new BitmapDrawable(getResources(), bitmap));
// This will not scale your Bitmap to fit it's bounds.
// You can set any other kind of Gravity, doesn't have to be center.
BitmapDrawable drawable = new BitmapDrawable(getResources(), bitmap);
drawable.setGravity(Gravity.CENTER);
parent.setBackground(drawable);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment