Add image utility methods

-Add convertCornerRadiusBitmap to edit bitmap cornor
-Add createIconWithDrawable to create icon from bitmap

Bug: 155822415
Test: manual test
Merged-In: Idb770ce5f3b9aaa1288538aa1e51ffc0d9f34a6e
Change-Id: Idb770ce5f3b9aaa1288538aa1e51ffc0d9f34a6e
(cherry picked from commit 37017a739c)
This commit is contained in:
timhypeng
2020-08-28 11:03:30 +08:00
committed by tim peng
parent 8a6e53837a
commit 7e04788dd1
2 changed files with 44 additions and 0 deletions

View File

@@ -13,6 +13,7 @@ import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.ColorMatrix;
@@ -32,6 +33,10 @@ import android.telephony.AccessNetworkConstants;
import android.telephony.NetworkRegistrationInfo;
import android.telephony.ServiceState;
import androidx.annotation.NonNull;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.UserIcons;
import com.android.launcher3.icons.IconFactory;
@@ -537,4 +542,25 @@ public class Utils {
== NetworkRegistrationInfo.REGISTRATION_STATE_ROAMING);
return !isInIwlan;
}
/**
* Returns a bitmap with rounded corner.
*
* @param context application context.
* @param source bitmap to apply round corner.
* @param cornerRadius corner radius value.
*/
public static Bitmap convertCornerRadiusBitmap(@NonNull Context context,
@NonNull Bitmap source, @NonNull float cornerRadius) {
final Bitmap roundedBitmap = Bitmap.createBitmap(source.getWidth(), source.getHeight(),
Bitmap.Config.ARGB_8888);
final RoundedBitmapDrawable drawable =
RoundedBitmapDrawableFactory.create(context.getResources(), source);
drawable.setAntiAlias(true);
drawable.setCornerRadius(cornerRadius);
final Canvas canvas = new Canvas(roundedBitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return roundedBitmap;
}
}

View File

@@ -18,6 +18,7 @@ import android.util.Log;
import android.util.Pair;
import androidx.annotation.DrawableRes;
import androidx.core.graphics.drawable.IconCompat;
import com.android.settingslib.R;
import com.android.settingslib.widget.AdaptiveIcon;
@@ -215,6 +216,23 @@ public class BluetoothUtils {
return new Pair<>(pair.first, pair.second);
}
/**
* Create an Icon pointing to a drawable.
*/
public static IconCompat createIconWithDrawable(Drawable drawable) {
Bitmap bitmap;
if (drawable instanceof BitmapDrawable) {
bitmap = ((BitmapDrawable) drawable).getBitmap();
} else {
final int width = drawable.getIntrinsicWidth();
final int height = drawable.getIntrinsicHeight();
bitmap = createBitmap(drawable,
width > 0 ? width : 1,
height > 0 ? height : 1);
}
return IconCompat.createWithBitmap(bitmap);
}
/**
* Build device icon with advanced outline
*/