Created
February 4, 2014 03:41
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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