下面列出了android.support.annotation.IntegerRes#android.graphics.LightingColorFilter 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 初始化相关数据
*
* @param recordingItem 音频数据源
* @param audioProgressColor 进度条颜色
*/
public void setData(RecordingItem recordingItem, int audioProgressColor) {
this.mRecordingItem = recordingItem;
// 设置进度条颜色
ColorFilter filter = new LightingColorFilter
(audioProgressColor, audioProgressColor);
mViewHolder.seekbar.getProgressDrawable().setColorFilter(filter);
mViewHolder.seekbar.getThumb().setColorFilter(filter);
if (!TextUtils.isEmpty(mRecordingItem.getFilePath())) {
mViewHolder.seekbar.setEnabled(true);
} else {
mViewHolder.seekbar.setEnabled(false);
}
initData();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void changeUIColor(Integer color) {
ValueAnimator anim = ValueAnimator.ofArgb(previsionThemeColor, color);
anim.setEvaluator(new ArgbEvaluator());
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
progressBar.getProgressDrawable().setColorFilter(new LightingColorFilter(0xFF000000, (Integer) valueAnimator.getAnimatedValue()));
setSystemBarColor((Integer) valueAnimator.getAnimatedValue());
navigation.setActiveTabColor((Integer) valueAnimator.getAnimatedValue());
fabSearch.setBackgroundTintList(ColorStateList.valueOf((Integer) valueAnimator.getAnimatedValue()));
}
});
anim.setDuration(getResources().getInteger(android.R.integer.config_shortAnimTime));
anim.start();
refreshLayout.setColorSchemeColors(color, color, color);
}
public Bitmap captureView(View view) {
//Find the view we are after
//Create a Bitmap with the same dimensions
Bitmap image = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(),
Bitmap.Config.ARGB_4444); //reduce quality and remove opacity
//Draw the view inside the Bitmap
Canvas canvas = new Canvas(image);
view.draw(canvas);
//Make it frosty
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF, 0x00222222); // lighten
//ColorFilter filter = new LightingColorFilter(0xFF7F7F7F, 0x00000000); // darken
paint.setColorFilter(filter);
canvas.drawBitmap(image, 0, 0, paint);
return image;
}
public Bitmap captureView(View view) {
if (mBlurredBitmap != null) {
return mBlurredBitmap;
}
//Find the view we are after
//Create a Bitmap with the same dimensions
mBlurredBitmap = Bitmap.createBitmap(view.getMeasuredWidth(),
view.getMeasuredHeight(),
Bitmap.Config.ARGB_4444); //reduce quality and remove opacity
//Draw the view inside the Bitmap
Canvas canvas = new Canvas(mBlurredBitmap);
view.draw(canvas);
//blur it
ImageHelper.blurBitmapWithRenderscript(rs, mBlurredBitmap);
//Make it frosty
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF, 0x00222222); // lighten
//ColorFilter filter = new LightingColorFilter(0xFF7F7F7F, 0x00000000); // darken
paint.setColorFilter(filter);
canvas.drawBitmap(mBlurredBitmap, 0, 0, paint);
return mBlurredBitmap;
}
/**
* Calculates the lighting of the item based on rotation.
*
* @param rotation The rotation of the item
* @return A color filter to use
*/
private LightingColorFilter calculateLight(final float rotation) {
final double cosRotation = Math.cos(Math.PI * rotation / 180);
int intensity = AMBIENT_LIGHT + (int)(DIFFUSE_LIGHT * cosRotation);
int highlightIntensity = (int)(SPECULAR_LIGHT * Math.pow(cosRotation, SHININESS));
if (intensity > MAX_INTENSITY) {
intensity = MAX_INTENSITY;
}
if (highlightIntensity > MAX_INTENSITY) {
highlightIntensity = MAX_INTENSITY;
}
final int light = Color.rgb(intensity, intensity, intensity);
final int highlight = Color.rgb(highlightIntensity, highlightIntensity, highlightIntensity);
return new LightingColorFilter(light, highlight);
}
public static Bitmap tint(Bitmap src, int color) {
// image size
int width = src.getWidth();
int height = src.getHeight();
// create output bitmap
// create a mutable empty bitmap
Bitmap bmOut = Bitmap.createBitmap(width, height, src.getConfig());
Paint p = new Paint(Color.RED);
ColorFilter filter = new LightingColorFilter(color, 1);
p.setColorFilter(filter);
Canvas c = new Canvas();
c.setBitmap(bmOut);
c.drawBitmap(src, 0, 0, p);
src.recycle();
src = null;
return bmOut;
}
private void createBitmap(Bitmap quarter) {
bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
// Top left
paint.setColorFilter(new LightingColorFilter(Color.RED, 0));
canvas.drawBitmap(quarter, 0, 0, paint);
// Top right
paint.setColorFilter(new LightingColorFilter(Color.YELLOW, 0));
canvas.drawBitmap(quarter, getWidth() / 2, 0, paint);
// Bottom left
paint.setColorFilter(new LightingColorFilter(Color.BLUE, 0));
canvas.drawBitmap(quarter, 0, getHeight()/2, paint);
// Bottom right
paint.setColorFilter(new LightingColorFilter(Color.GREEN, 0));
canvas.drawBitmap(quarter, getWidth() / 2, getHeight() / 2, paint);
}
/**
* Generate label background and foreground colors using Palette base on downloaded image colors.
*
* @param bitmap Download bitmap.
*/
@Override
public void onBitmapChange(Bitmap bitmap) {
if (bitmap == null) { return; }
Palette.generateAsync(bitmap, new Palette.PaletteAsyncListener() {
@SuppressWarnings("deprecation")
public void onGenerated(Palette palette) {
Resources res = getResources();
int photoNameColorBg = palette.getDarkMutedColor(res.getColor(R.color.list_item_photo_name_bg));
int photoNameColorFg = palette.getLightMutedColor(res.getColor(R.color.view_photo_name_fg));
ColorFilter photoNameColorFilter = new LightingColorFilter(photoNameColorBg, 1);
Drawable photoNameDrawableBg = res.getDrawable(R.drawable.view_photo_name_bg);
photoNameDrawableBg.setColorFilter(photoNameColorFilter);
mPhotoName.setBackgroundDrawable(photoNameDrawableBg);
mPhotoName.setTextColor(photoNameColorFg);
}
});
}
private LightingColorFilter calculateLight(final float rotation) {
final double cosRotation = Math.cos(Math.PI * rotation / 180);
int intensity = AMBIENT_LIGHT + (int) (DIFFUSE_LIGHT * cosRotation);
int highlightIntensity = (int) (SPECULAR_LIGHT * Math.pow(cosRotation,
SHININESS));
if (intensity > MAX_INTENSITY) {
intensity = MAX_INTENSITY;
}
if (highlightIntensity > MAX_INTENSITY) {
highlightIntensity = MAX_INTENSITY;
}
final int light = Color.rgb(intensity, intensity, intensity);
final int highlight = Color.rgb(highlightIntensity, highlightIntensity,
highlightIntensity);
return new LightingColorFilter(light, highlight);
}
private LightingColorFilter calculateLight(final float rotation) {
final double cosRotation = Math.cos(Math.PI * rotation / 180);
int intensity = AMBIENT_LIGHT + (int) (DIFFUSE_LIGHT * cosRotation);
int highlightIntensity = (int) (SPECULAR_LIGHT * Math.pow(cosRotation,
SHININESS));
if (intensity > MAX_INTENSITY) {
intensity = MAX_INTENSITY;
}
if (highlightIntensity > MAX_INTENSITY) {
highlightIntensity = MAX_INTENSITY;
}
final int light = Color.rgb(intensity, intensity, intensity);
final int highlight = Color.rgb(highlightIntensity, highlightIntensity,
highlightIntensity);
return new LightingColorFilter(light, highlight);
}
@Override
public void process(Bitmap dst, Bitmap src) {
super.process(dst, src);
final Canvas canvas = new Canvas(dst);
final Paint paint = new Paint();
paint.setColorFilter(new LightingColorFilter(mMul, mAdd));
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(src, 0, 0, paint);
}
public Wave(View v, Bitmap b, int durationMillis, int color) {
bitmap = b;
shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
paint = new Paint();
paint.setShader(shader);
paint.setAntiAlias(true);
matrix = new Matrix();
durMillis = durationMillis;
viewRef = new WeakReference<>(v);
if (color != 0) {
paint.setColorFilter(new LightingColorFilter(0X02FFFFFF, color));
}
}
private Bitmap changeBitmapColor(Bitmap sourceBitmap, int color) {
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,
sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1);
Paint p = new Paint();
ColorFilter filter = new LightingColorFilter(color, 1);
p.setColorFilter(filter);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, p);
return resultBitmap;
}
public static Bitmap getRoundedCornerAndLightenBitmap(Bitmap bitmap, int cornerRadiusInPixels, boolean captureCircle) {
Bitmap output = Bitmap.createBitmap(
bitmap.getWidth(),
bitmap.getHeight(),
Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(output);
final int color = 0xffffffff;
final Paint paint = new Paint();
final Rect rect = new Rect(0,
0,
bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = cornerRadiusInPixels;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
if (captureCircle) {
canvas.drawCircle(rectF.centerX(),
rectF.centerY(),
bitmap.getWidth() / 2,
paint);
} else {
canvas.drawRoundRect(rectF,
roundPx,
roundPx,
paint);
}
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
ColorFilter filter = new LightingColorFilter(0xFFFFFFFF, 0x00222222); // lighten
//ColorFilter filter = new LightingColorFilter(0xFF7F7F7F, 0x00000000); // darken
paint.setColorFilter(filter);
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
public void setProgressBar(ProgressBar progressBar) {
progressBar.getIndeterminateDrawable().setColorFilter(new LightingColorFilter(0xFF000000,
ContextCompat.getColor(getActivity(), android.R.color.white)));
ActionBar actionBar;
if ((actionBar = getActionBar()) != null) {
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(progressBar, new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.END));
}
}
private void init() {
starBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.star);
goldPaint = new Paint();
goldPaint.setFilterBitmap(true);
grayPaint = new Paint();
grayPaint.setColorFilter(new LightingColorFilter(0xff111122, 0xffcccccc));
grayPaint.setFilterBitmap(true);
setModel(new StarRatingModel());
}
@Override
public void draw(final Canvas canvas) {
final Paint paint = getPaint();
if (paint.getColorFilter() == null) {
paint.setColorFilter(new LightingColorFilter(tint, 0));
paint.setAlpha(alpha);
}
super.draw(canvas);
}
public void setProgressBar(ProgressBar progressBar) {
progressBar.getIndeterminateDrawable().setColorFilter(new LightingColorFilter(0xFF000000,
getResources().getColor(android.R.color.white)));
ActionBar actionBar;
if ((actionBar = getActionBar()) != null) {
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(progressBar, new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.END));
}
}
/**
* 给bitmap着色
* @param sourceBitmap
* @param color rgb
* @return
*/
public static Bitmap changeBitmapColor(@NonNull Bitmap sourceBitmap, @IntegerRes int color) {
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,
sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1);
Paint p = new Paint();
ColorFilter filter = new LightingColorFilter(color, 1);
p.setColorFilter(filter);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, p);
return resultBitmap;
}
/**
* White colors can be transformed in to different colors.
*
* @param sourceBitmap The source Bitmap
* @param color The different color
* @return The different color Bitmap
*/
public static Bitmap changeImageColor(Bitmap sourceBitmap, int color) {
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth() - 1,
sourceBitmap.getHeight() - 1);
Paint p = new Paint();
ColorFilter filter = new LightingColorFilter(color, 1);
p.setColorFilter(filter);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, p);
return resultBitmap;
}
/**
* White colors can be transformed in to different colors.
*
* @param sourceBitmap The source Bitmap
* @param color The different color
* @return The different color Bitmap
*/
public static Bitmap changeImageColor(Bitmap sourceBitmap, int color) {
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth() - 1,
sourceBitmap.getHeight() - 1);
Paint p = new Paint();
ColorFilter filter = new LightingColorFilter(color, 1);
p.setColorFilter(filter);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, p);
return resultBitmap;
}
public void setCustomBackground(Drawable d) {
mCustomBackground = d;
if (d != null && d instanceof BitmapDrawable) {
// This is to add a tint of the background color to the image.
// It prevents overly exposed or bright backgrounds from ruining the ambiance.
BitmapDrawable bd = (BitmapDrawable) d;
bd.getPaint().setColor(backgroundColor);
ColorFilter filter = new LightingColorFilter(backgroundColor, 1);
bd.setColorFilter(filter);
}
setBackground(mBackgroundDrawable);
computeCustomBackgroundBounds();
invalidate();
}
/**
* Creates a tinted copy of the supplied bitmap.
* @param b a bitmap image
* @param color a color
* @return a bitmap tinted to color
*/
public static Bitmap tintBitmap(Bitmap b, int color)
{
Bitmap tinted = Bitmap.createBitmap(b.getWidth(), b.getHeight(), b.getConfig());
Canvas c = new Canvas(tinted);
Paint p = new Paint();
p.setColorFilter(new LightingColorFilter(color, 0));
c.drawBitmap(b, 0, 0, p);
return tinted;
}
@Override
public void draw(final Canvas canvas) {
final Paint paint = getPaint();
if (paint.getColorFilter() == null) {
paint.setColorFilter(new LightingColorFilter(tint, 0));
paint.setAlpha(alpha);
}
super.draw(canvas);
}
public LightingColorFilterView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0x00FF00FF));
bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
}
private void drawCheckedMark(Canvas canvas) {
Paint paint = new Paint();
paint.setColorFilter(new LightingColorFilter(getDrawableTintColor(), 0));
Bitmap bitmap = Bitmap.createScaledBitmap(originalFinishedBitmap, imageSize, imageSize, true);
canvas.drawBitmap(bitmap, parentCenter - bitmap.getWidth() / 2,
parentCenter - bitmap.getHeight() / 2, paint);
}
public static Bitmap changeBitmapColor(Bitmap bmp, int color) {
Bitmap result = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), bmp.getConfig());
Canvas c = new Canvas(result);
Paint paint = new Paint();
ColorFilter filter = new LightingColorFilter(color, 1);
paint.setColorFilter(filter);
c.drawBitmap(bmp, 0, 0, paint);
return result;
}
public static Drawable getTickedButtonDrawable(Context context, int tickButtonColor) {
// Prepare the layers & color filter for the LayerDrawable
ColorFilter cf = new LightingColorFilter(tickButtonColor, 0);
//ColorDrawable buttonCenterDrawable = new ColorDrawable(0xFF000000 + tickButtonColor);
Drawable buttonCenterDrawable = ContextCompat.getDrawable(context, R.drawable.mask_64);
Drawable buttonBorderDrawable = ContextCompat.getDrawable(context, R.drawable.on_64);
buttonCenterDrawable.setColorFilter(cf);
sTickedButton = new LayerDrawable(new Drawable[]{buttonCenterDrawable, buttonBorderDrawable});
return sTickedButton;
}
public static View.OnTouchListener getHighlightTouchListener(final int color) {
return new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
Drawable drawable = ((ImageView) view).getDrawable();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
LightingColorFilter lighten = new LightingColorFilter(color, color);
drawable.setColorFilter(lighten);
break;
case MotionEvent.ACTION_UP:
drawable.clearColorFilter();
break;
case MotionEvent.ACTION_MOVE:
Rect rect = new Rect();
view.getLocalVisibleRect(rect);
if (!rect.contains((int) event.getX(), (int) event.getY())) {
drawable.clearColorFilter();
}
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
drawable.clearColorFilter();
break;
}
return false;
}
};
}
public void setCustomBackground(Drawable d) {
mCustomBackground = d;
if (d != null && d instanceof BitmapDrawable) {
// This is to add a tint of the background color to the image.
// It prevents overly exposed or bright backgrounds from ruining the ambiance.
BitmapDrawable bd = (BitmapDrawable) d;
bd.getPaint().setColor(backgroundColor);
ColorFilter filter = new LightingColorFilter(backgroundColor, 1);
bd.setColorFilter(filter);
}
setBackground(mBackgroundDrawable);
computeCustomBackgroundBounds();
invalidate();
}