简体   繁体   中英

How to resize a Bitmap image in Android

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="32dp"
    android:paddingTop="10dp"
    android:paddingRight="0dp"
    android:paddingBottom="10dp">
    <item android:bottom="3dp">
        <bitmap
            android:gravity="center_vertical|left"
            android:src="@drawable/ic_publish_black_24dp" />
        <shape android:shape="rectangle">
            <stroke
                android:width="0dp"
                android:color="#37474f" />
        </shape>
    </item>
</layer-list>  

This is how I call it:

TextView aboutBags = new TextView( mContext );
aboutBags.setText("Compose");
aboutBags.setTextAppearance( R.style.RevStyleHelpAbout );
aboutBags.setBackgroundResource( R.drawable.rev_item_h_border_left );  

I've tried border attributes android:width="22dp" android:height="22dp" all over the XML, but the image doesn't get resized.

How can I resize the bitmap image?

Vielen dank im voraus.

Using Bitmap may help you. Use like this:

Bitmap nameYourBitmap;

Bitmap resized = Bitmap.createScaledBitmap(nameYourBitmap, newWidth, newHeight, true);

You can also do like this:

resized = Bitmap.createScaledBitmap(nameYourBitmap,(int)(nameYourBitmap.getWidth()*0.8), (int)(nameYourBitmap.getHeight()*0.8), true);

Refer to this: How to resize Image in Android?

For XML, the scale attributes are defined for ScaleDrawables, and they can scale another drawable. Please refer to this developer site: https://developer.android.com/guide/topics/resources/drawable-resource.html#Scale

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM