问题
My requirement is to display half of a wheel image at bottom of the screen. I can't cut the image, as I need to rotate it at run time. I have tried different options with relative layout(by setting this as alignParentBottom as true and by giving -ve margin bottom), linear layout(by giving margins). Its working for normal mdpi(320X480), hdpi(480X800) devices. But when device's height increases like for (like 480X870) then the entire wheel is getting displayed.
Wheel Image is
and the screen should look like as
Need the help regarding this. Thanks in advance.
回答1:
You could always crop the piece that you need via the Bitmap.createBitmap()
static method, and then assign it to the view:
Something like...
// Set some constants
private static final Bitmap SOURCE_BITMAP = BitmapFactory.decodeFile(....); // Get the source Bitmap using your favorite method :-)
private static final int START_X = 10;
private static final int START_Y = 15;
private static final int WIDTH_PX = 100;
private static final int HEIGHT_PX = 100;
// Crop bitmap
Bitmap newBitmap = Bitmap.createBitmap(SOURCE_BITMAP, START_X, START_Y, WIDTH_PX, HEIGHT_PX, null, false);
// Assign new bitmap to ImageView
ImageView image = (ImageView)findViewById(R.id.image_view);
image.setImageBitmap(newBitmap);
In here, your SOURCE_BITMAP
is your original steering image.
I hope, this helps.
回答2:
set android:translationY="300dp" in xml file.
That will move image to bottom side
回答3:
First set imageview align bottom in relative layout in xml file. Then set imageview margin programmatically by half (or ratio you want) of imageview height.
int height = imageview.getHeight();
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
params.setMargins(0, height/2, 0, 0);
imageview.setLayoutParams(params);
来源:https://stackoverflow.com/questions/16436863/display-half-of-the-image-at-the-bottom-of-the-screen