下面列出了android.graphics.Paint#setFilterBitmap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressLint("NewApi")
protected void initBaseZoomableImageView( Context context) {
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
if(context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
landscape = true;
}else {
landscape = false;
}
// Setup the refresh runnable
mRefresh = new Runnable() {
@Override
public void run() {
postInvalidate();
}
};
}
/**
* Use touch-icon or higher-resolution favicon and round the corners.
* @param context Context used to get resources.
* @param touchIcon Touch icon bitmap.
* @param canvas Canvas that holds the touch icon.
*/
private static void drawTouchIconToCanvas(
Context context, Bitmap touchIcon, Canvas canvas) {
Rect iconBounds = new Rect(0, 0, canvas.getWidth(), canvas.getHeight());
Rect src = new Rect(0, 0, touchIcon.getWidth(), touchIcon.getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setFilterBitmap(true);
canvas.drawBitmap(touchIcon, src, iconBounds, paint);
// Convert dp to px.
int borderRadii = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
TOUCHICON_BORDER_RADII, context.getResources().getDisplayMetrics());
Path path = new Path();
path.setFillType(Path.FillType.INVERSE_WINDING);
RectF rect = new RectF(iconBounds);
rect.inset(INSET_DIMENSION_FOR_TOUCHICON, INSET_DIMENSION_FOR_TOUCHICON);
path.addRoundRect(rect, borderRadii, borderRadii, Path.Direction.CW);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
canvas.drawPath(path, paint);
}
@SuppressLint("NewApi")
protected void initBaseZoomableImageView(Context context) {
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setFilterBitmap(true);
mPaint.setAntiAlias(true);
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
landscape = true;
} else {
landscape = false;
}
// Setup the refresh runnable
mRefresh = new Runnable() {
@Override
public void run() {
postInvalidate();
}
};
}
/**
* 将图片涂色
*
* @param drawableId 图片
* @param color 颜色
* @return
*/
private Bitmap casualStroke(int drawableId, int color) {
Bitmap mode = ((BitmapDrawable) this.getResources().getDrawable(
drawableId)).getBitmap();
Bitmap bitmap = mode.copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas();
canvas.setBitmap(bitmap);
Paint paintUnder = new Paint();
paintUnder.setColor(color);
canvas.drawPaint(paintUnder);
Paint paint = new Paint();
paint.setFilterBitmap(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
canvas.drawBitmap(mode, 0, 0, paint);
return bitmap;
}
@Override
protected void init() {
setFocusable(true);
setClickable(true);
setWillNotDraw(false);
x = new AnimatedInteger(-1);
y = new AnimatedInteger(-1);
circleWidth = DimenUtils.dpToPx(18);
paint = new Paint();
paint.setDither(true);
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
fillPaint = new Paint();
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setAntiAlias(true);
strokePaint = new Paint();
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setStrokeWidth(DimenUtils.dpToPx(2));
strokePaint.setAntiAlias(true);
bitmapMatrix = new Matrix();
getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (bitmap != null)
calculateBitmapMatrix();
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
private void initPaint()
{
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(6);
mPaint.setColor(Color.BLACK);
mPaint.setDither(true);
mPaint.setFilterBitmap(true);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeJoin(Paint.Join.ROUND);
}
private void initPaint() {
mBitPaint = new Paint();
// 防抖动
mBitPaint.setDither(true);
// 开启图像过滤
mBitPaint.setFilterBitmap(true);
}
/**
* 初始化画笔
*/
private void initPaint()
{
mFullPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mFullPaint.setStyle(Paint.Style.FILL);
mFullPaint.setStrokeWidth(2);
mFullPaint.setColor(Color.WHITE);
mFullPaint.setDither(true);
mFullPaint.setFilterBitmap(true);
}
private static void scale(Bitmap src, Bitmap dest, int clearColor,
int x, int y, int sw, int sh,
int dx, int dy,
int dw, int dh) {
dest.eraseColor(clearColor);
Canvas canvas = new Canvas(dest);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.drawBitmap(src, new Rect(x + 1, y + 1, sw - 1, sh - 1), new Rect(dx, dy, dw, dh), paint);
canvas.setBitmap(null);
}
/**
* Adapts a website's icon (e.g. favicon or touch icon) to make it suitable for the home screen.
* This involves adding padding if the icon is a full sized square.
*
* @param context Context used to create the intent.
* @param webIcon The website's favicon or touch icon.
* @return Bitmap Either the touch-icon or the newly created favicon.
*/
@CalledByNative
public static Bitmap createHomeScreenIconFromWebIcon(Bitmap webIcon) {
// getLauncherLargeIconSize() is just a guess at the launcher icon size, and is often
// wrong -- the launcher can show icons at any size it pleases. Instead of resizing the
// icon to the supposed launcher size and then having the launcher resize the icon again,
// just leave the icon at its original size and let the launcher do a single rescaling.
// Unless the icon is much too big; then scale it down here too.
Context context = ContextUtils.getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
int maxInnerSize = Math.round(am.getLauncherLargeIconSize() * MAX_INNER_SIZE_RATIO);
int innerSize = Math.min(maxInnerSize, Math.max(webIcon.getWidth(), webIcon.getHeight()));
int outerSize = innerSize;
Rect innerBounds = new Rect(0, 0, innerSize, innerSize);
// Draw the icon with padding around it if all four corners are not transparent. Otherwise,
// don't add padding.
if (shouldPadIcon(webIcon)) {
int padding = Math.round(ICON_PADDING_RATIO * innerSize);
outerSize += 2 * padding;
innerBounds.offset(padding, padding);
}
Bitmap bitmap = null;
try {
bitmap = Bitmap.createBitmap(outerSize, outerSize, Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError e) {
Log.e(TAG, "OutOfMemoryError while creating bitmap for home screen icon.");
return webIcon;
}
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setFilterBitmap(true);
canvas.drawBitmap(webIcon, null, innerBounds, paint);
return bitmap;
}
private Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius){
Bitmap finalBitmap;
if( bitmap.getWidth() != radius || bitmap.getHeight() != radius ){
finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius, false);
} else{
finalBitmap = bitmap;
}
Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(), finalBitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0,0, finalBitmap.getWidth(), finalBitmap.getHeight());
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.parseColor("#BAB399"));
canvas.drawCircle(finalBitmap.getWidth() / 2 + 0.7f,
finalBitmap.getHeight() / 2 + 0.7f,
finalBitmap.getWidth() / 2 + 0.1f, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(finalBitmap, rect, rect, paint);
return output;
}
public ClippingImageView(Context context) {
super(context);
paint = new Paint(Paint.FILTER_BITMAP_FLAG);
paint.setFilterBitmap(true);
matrix = new Matrix();
drawRect = new RectF();
bitmapRect = new RectF();
roundPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
roundRect = new RectF();
shaderMatrix = new Matrix();
}
private static Paint createAntiAliasingPaint() {
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setFilterBitmap(true);
paint.setDither(true);
return paint;
}
/**
* 初始化画笔
*
* @param lineWidth 线宽
*/
protected void initPaint(float lineWidth)
{
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeWidth(lineWidth);
mPaint.setColor(Color.BLACK);
mPaint.setDither(true);
mPaint.setFilterBitmap(true);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeJoin(Paint.Join.ROUND);
}
private void init() {
setDrawingCacheEnabled(true);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setFilterBitmap(true);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mDrawSize = DimenUtils.dp2pxInt(3);
mEraserSize = DimenUtils.dp2pxInt(30);
mPaint.setStrokeWidth(mDrawSize);
mPaint.setColor(0XFF000000);
mXferModeDraw = new PorterDuffXfermode(PorterDuff.Mode.SRC);
mXferModeClear = new PorterDuffXfermode(PorterDuff.Mode.CLEAR);
mPaint.setXfermode(mXferModeDraw);
}
private void drawPatch(Canvas c, float x, float y, float size,
int patch, int turn, boolean invert, int fillColor,
int strokeColor) {
patch %= patchTypes.length;
turn %= 4;
if ((patchFlags[patch] & PATCH_INVERTED) != 0)
invert = !invert;
Path shape = patchShapes[patch];
float scale = size / patchSize;
float offset = size / 2.0f;
// setup our paint
Paint p = new Paint();
p.setStyle(Paint.Style.FILL_AND_STROKE);
p.setAntiAlias(true);
p.setFilterBitmap(true);
// save the current matrix
c.save();
c.translate(x + offset, y + offset);
c.scale(scale, scale);
c.rotate(turn * 90f);
// draw the patch
p.setColor(strokeColor);
c.drawPath(shape, p);
// restore the saved matrix
c.restore();
}
private void initPaint()
{
mFullPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mFullPaint.setStyle(Paint.Style.FILL);
mFullPaint.setColor(Color.BLACK);
mFullPaint.setDither(true);
mFullPaint.setFilterBitmap(true);
mFullPaint.setStrokeCap(Paint.Cap.ROUND);
mFullPaint.setStrokeJoin(Paint.Join.ROUND);
}
static void update1x1(Context context, AppWidgetManager appWidgetManager, int widgetId) {
Resources r = context.getResources();
float dp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, r.getDisplayMetrics());
LocalDateTime now = LocalDateTime.now();
LocalDate today = now.toLocalDate();
Theme theme = WidgetUtils.getTheme(widgetId);
Times times = WidgetUtils.getTimes(widgetId);
if (times == null) {
WidgetUtils.showNoCityWidget(context, appWidgetManager, widgetId);
return;
}
WidgetUtils.Size size = WidgetUtils.getSize(context, appWidgetManager, widgetId, 1f);
int s = size.width;
if (s <= 0)
return;
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.vakit_widget);
int next = times.getNextTime();
String left = LocaleUtils.formatPeriod(now, times.getTime(today, next), false);
if (Preferences.VAKIT_INDICATOR_TYPE.get().equals("next"))
next = next + 1;
remoteViews.setOnClickPendingIntent(R.id.widget, TimesFragment.getPendingIntent(times));
Bitmap bmp = Bitmap.createBitmap(s, s, Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bmp);
canvas.scale(0.99f, 0.99f, s / 2f, s / 2f);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
paint.setFilterBitmap(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(theme.bgcolor);
canvas.drawRect(0, 0, s, s, paint);
paint.setColor(theme.textcolor);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setAntiAlias(true);
paint.setSubpixelText(true);
paint.setColor(theme.hovercolor);
String city = times.getName();
paint.setColor(theme.textcolor);
float cs = s / 5f;
float ts = (s * 35) / 100f;
int vs = s / 4;
paint.setTextSize(cs);
cs = (cs * s * 0.9f) / paint.measureText(city);
cs = (cs > vs) ? vs : cs;
paint.setTextSize(vs);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(Vakit.getByIndex(next).prevTime().getString(), s / 2f, (s * 22) / 80f, paint);
paint.setTextSize(ts);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(left, s / 2f, (s / 2f) + ((ts * 1) / 3), paint);
paint.setTextSize(cs);
paint.setTextAlign(Paint.Align.CENTER);
canvas.drawText(city, s / 2f, ((s * 3) / 4f) + ((cs * 2) / 3), paint);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(dp);
paint.setColor(theme.strokecolor);
canvas.drawRect(0, 0, s, s, paint);
remoteViews.setImageViewBitmap(R.id.widget, bmp);
try {
appWidgetManager.updateAppWidget(widgetId, remoteViews);
} catch (RuntimeException e) {
if (!e.getMessage().contains("exceeds maximum bitmap memory usage")) {
Crashlytics.logException(e);
}
}
}
public ShimmerFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setWillNotDraw(false);
mMask = new Mask();
mAlphaPaint = new Paint();
mMaskPaint = new Paint();
mMaskPaint.setAntiAlias(true);
mMaskPaint.setDither(true);
mMaskPaint.setFilterBitmap(true);
mMaskPaint.setXfermode(DST_IN_PORTER_DUFF_XFERMODE);
useDefaults();
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ShimmerFrameLayout, 0, 0);
try {
if (a.hasValue(R.styleable.ShimmerFrameLayout_auto_start)) {
setAutoStart(a.getBoolean(R.styleable.ShimmerFrameLayout_auto_start, false));
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_base_alpha)) {
setBaseAlpha(a.getFloat(R.styleable.ShimmerFrameLayout_base_alpha, 0));
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_duration)) {
setDuration(a.getInt(R.styleable.ShimmerFrameLayout_duration, 0));
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_repeat_count)) {
setRepeatCount(a.getInt(R.styleable.ShimmerFrameLayout_repeat_count, 0));
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_repeat_delay)) {
setRepeatDelay(a.getInt(R.styleable.ShimmerFrameLayout_repeat_delay, 0));
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_repeat_mode)) {
setRepeatMode(a.getInt(R.styleable.ShimmerFrameLayout_repeat_mode, 0));
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_angle)) {
int angle = a.getInt(R.styleable.ShimmerFrameLayout_angle, 0);
switch (angle) {
default:
case 0:
mMask.angle = MaskAngle.CW_0;
break;
case 90:
mMask.angle = MaskAngle.CW_90;
break;
case 180:
mMask.angle = MaskAngle.CW_180;
break;
case 270:
mMask.angle = MaskAngle.CW_270;
break;
}
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_shape)) {
int shape = a.getInt(R.styleable.ShimmerFrameLayout_shape, 0);
switch (shape) {
default:
case 0:
mMask.shape = MaskShape.LINEAR;
break;
case 1:
mMask.shape = MaskShape.RADIAL;
break;
}
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_dropoff)) {
mMask.dropoff = a.getFloat(R.styleable.ShimmerFrameLayout_dropoff, 0);
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_fixed_width)) {
mMask.fixedWidth = a.getDimensionPixelSize(R.styleable.ShimmerFrameLayout_fixed_width, 0);
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_fixed_height)) {
mMask.fixedHeight = a.getDimensionPixelSize(R.styleable.ShimmerFrameLayout_fixed_height, 0);
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_intensity)) {
mMask.intensity = a.getFloat(R.styleable.ShimmerFrameLayout_intensity, 0);
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_relative_width)) {
mMask.relativeWidth = a.getFloat(R.styleable.ShimmerFrameLayout_relative_width, 0);
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_relative_height)) {
mMask.relativeHeight = a.getFloat(R.styleable.ShimmerFrameLayout_relative_height, 0);
}
if (a.hasValue(R.styleable.ShimmerFrameLayout_tilt)) {
mMask.tilt = a.getFloat(R.styleable.ShimmerFrameLayout_tilt, 0);
}
} finally {
a.recycle();
}
}
}
/**
* set graph line color
*/
private void setPaint() {
pGraphColor = new Paint();
pGraphColor.setFlags(Paint.ANTI_ALIAS_FLAG);
pGraphColor.setAntiAlias(true); //text anti alias
pGraphColor.setFilterBitmap(true); // bitmap anti alias
pGraphColor.setColor(Color.BLUE);
pGraphColor.setStrokeWidth(3);
pGraphColor.setStyle(Style.STROKE);
pGraphRegionColor = new Paint();
pGraphRegionColor.setFlags(Paint.ANTI_ALIAS_FLAG);
pGraphRegionColor.setAntiAlias(true); //text anti alias
pGraphRegionColor.setFilterBitmap(true); // bitmap anti alias
pGraphRegionColor.setColor(Color.BLUE);
pGraphRegionColor.setAlpha(127);
pGraphRegionColor.setStrokeWidth(1);
pGraphRegionColor.setStyle(Style.FILL_AND_STROKE);
pCircle = new Paint();
pCircle.setFlags(Paint.ANTI_ALIAS_FLAG);
pCircle.setAntiAlias(true); //text anti alias
pCircle.setFilterBitmap(true); // bitmap anti alias
pCircle.setColor(Color.BLUE);
pCircle.setStrokeWidth(3);
pCircle.setStyle(Style.FILL_AND_STROKE);
pCrossLine = new Paint();
pCrossLine.setFlags(Paint.ANTI_ALIAS_FLAG);
pCrossLine.setAntiAlias(true); //text anti alias
pCrossLine.setFilterBitmap(true); // bitmap anti alias
pCrossLine.setColor(Color.GRAY);
pCrossLine.setStrokeWidth(3);
pBaseLine = new Paint();
pBaseLine.setFlags(Paint.ANTI_ALIAS_FLAG);
pBaseLine.setAntiAlias(true); //text anti alias
pBaseLine.setFilterBitmap(true); // bitmap anti alias
pBaseLine.setColor(0xffcccccc);
pBaseLine.setStrokeWidth(3);
pBaseLine.setStyle(Style.STROKE);
pBaseLine.setPathEffect(new DashPathEffect(new float[] {10,5}, 0));
pMarkText = new Paint();
pMarkText.setFlags(Paint.ANTI_ALIAS_FLAG);
pMarkText.setAntiAlias(true); //text anti alias
pMarkText.setColor(Color.BLACK);
}