How to display a part of an image?

后端 未结 2 1296
温柔的废话
温柔的废话 2020-12-29 00:06

I have a large image. But i have to display just some portion of it. How is this possible using Android ImageView?

相关标签:
2条回答
  • 2020-12-29 00:39

    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);
    
    0 讨论(0)
  • 2020-12-29 00:57

    Use android:scaleType="centerInside"

    0 讨论(0)
提交回复
热议问题