下面列出了android.graphics.Paint#setShadowLayer ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Initialize all the Resources needed before drawing.
*
* @param FloatingActionButtonColor
*/
public void init(int FloatingActionButtonColor) {
setWillNotDraw(false);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mButtonPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mButtonPaint.setColor(FloatingActionButtonColor);
mButtonPaint.setStyle(Paint.Style.FILL);
mButtonPaint.setShadowLayer(10.0f,
0.0f,
3.5f,
Color.argb(100,
0,
0,
0));
mDrawablePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
invalidate();
}
private RectF drawBoxWithShadow(Canvas c, Paint p, int width, int height) {
Resources res = mContext.getResources();
float shadowBlur = res.getDimension(R.dimen.widget_preview_shadow_blur);
float keyShadowDistance = res.getDimension(R.dimen.widget_preview_key_shadow_distance);
float corner = res.getDimension(R.dimen.widget_preview_corner_radius);
RectF bounds = new RectF(shadowBlur, shadowBlur,
width - shadowBlur, height - shadowBlur - keyShadowDistance);
p.setColor(Color.WHITE);
// Key shadow
p.setShadowLayer(shadowBlur, 0, keyShadowDistance,
ShadowGenerator.KEY_SHADOW_ALPHA << 24);
c.drawRoundRect(bounds, corner, corner, p);
// Ambient shadow
p.setShadowLayer(shadowBlur, 0, 0,
ColorUtils.setAlphaComponent(Color.BLACK, ShadowGenerator.AMBIENT_SHADOW_ALPHA));
c.drawRoundRect(bounds, corner, corner, p);
p.clearShadowLayer();
return bounds;
}
Dot(DotLoader parent, int dotRadius, int position) {
this.position = position;
mColors = parent.mColors;
mCurrentColorIndex = 0;
mDotRadius = dotRadius;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(mColors[mCurrentColorIndex]);
mPaint.setShadowLayer(5.5f, 6.0f, 6.0f, Color.BLACK);
mPaint.setStyle(Paint.Style.FILL);
}
private void initializeWatchFace() {
mHourPaint = new Paint();
mHourPaint.setColor(mWatchHandAndComplicationsColor);
mHourPaint.setStrokeWidth(HOUR_STROKE_WIDTH);
mHourPaint.setAntiAlias(true);
mHourPaint.setStrokeCap(Paint.Cap.ROUND);
mHourPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mMinutePaint = new Paint();
mMinutePaint.setColor(mWatchHandAndComplicationsColor);
mMinutePaint.setStrokeWidth(MINUTE_STROKE_WIDTH);
mMinutePaint.setAntiAlias(true);
mMinutePaint.setStrokeCap(Paint.Cap.ROUND);
mMinutePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mSecondAndHighlightPaint = new Paint();
mSecondAndHighlightPaint.setColor(mWatchHandHighlightColor);
mSecondAndHighlightPaint.setStrokeWidth(SECOND_TICK_STROKE_WIDTH);
mSecondAndHighlightPaint.setAntiAlias(true);
mSecondAndHighlightPaint.setStrokeCap(Paint.Cap.ROUND);
mSecondAndHighlightPaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
mTickAndCirclePaint = new Paint();
mTickAndCirclePaint.setColor(mWatchHandAndComplicationsColor);
mTickAndCirclePaint.setStrokeWidth(SECOND_TICK_STROKE_WIDTH);
mTickAndCirclePaint.setAntiAlias(true);
mTickAndCirclePaint.setStyle(Paint.Style.STROKE);
mTickAndCirclePaint.setShadowLayer(SHADOW_RADIUS, 0, 0, mWatchHandShadowColor);
}
private void setupPaint() {
paintIcons = new Paint();
paintIcons.setAlpha(0);
paintFAB = new Paint();
paintFAB.setStyle(Paint.Style.FILL);
paintFAB.setColor(fabColor);
paintFAB.setShadowLayer(0, 0, 0, Color.BLACK);
// Important for certain APIs
setLayerType(LAYER_TYPE_SOFTWARE, paintFAB);
paintFabBGWidShadow = new Paint();
paintFabBGWidShadow.setStyle(Paint.Style.FILL);
paintFabBGWidShadow.setColor(Color.GREEN);
paintFabBGWidShadow.setShadowLayer(5, 1, 1, Color.BLACK);
setLayerType(LAYER_TYPE_SOFTWARE, paintFabBGWidShadow);
paintFabBGWithoutShadow = new Paint();
paintFabBGWithoutShadow.setStyle(Paint.Style.FILL);
paintFabBGWithoutShadow.setColor(Color.TRANSPARENT);
paintFabBG = paintFabBGWidShadow;
paintFabPlus = new Paint();
paintFabPlus.setColor(Color.WHITE);
paintFabPlus.setAntiAlias(true);
paintFabPlus.setStrokeWidth(FAB_ICON_WIDTH);
}
public void init(int FloatingActionButtonColor) {
setWillNotDraw(false);
setLayerType(View.LAYER_TYPE_SOFTWARE, null);
mButtonPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mButtonPaint.setColor(FloatingActionButtonColor);
mButtonPaint.setStyle(Paint.Style.FILL);
mButtonPaint.setShadowLayer(10.0f, 0.0f, 3.5f, Color.argb(100, 0, 0, 0));
mDrawablePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
invalidate();
}
private void setBasics() {
this.drawLock = false;
space = Utils.getDpPixels(6.0f);
// background image
bitCompassBg = Images.getImageD(R.drawable.var_skyplot);
// load other images
int imgSize = (int) Utils.getDpPixels(20);
satImages = new Bitmap[3];
satImages[0] = Images.getImageB(R.drawable.ic_sat_01, imgSize);
satImages[1] = Images.getImageB(R.drawable.ic_sat_02, imgSize);
satImages[2] = Images.getImageB(R.drawable.ic_sat_03, imgSize);
mPaintBitmap = new Paint();
mPaintBitmap.setAntiAlias(true);
mPaintBitmap.setFilterBitmap(true);
mPaintText = new Paint();
mPaintText.setAntiAlias(true);
mPaintText.setTextAlign(Align.CENTER);
mPaintText.setTextSize(SAT_TEXT_SIZE);
mPaintText.setShadowLayer(SAT_TEXT_SIZE / 4.0f, 0, 0, Color.WHITE);
mPaintSignalLine = new Paint();
mPaintSignalLine.setAntiAlias(true);
mPaintSignalLine.setStyle(Style.STROKE);
mPaintSignalLine.setStrokeWidth(2.0f);
mPaintSignalLine.setColor(Color.GRAY);
}
/**
* 添加水印分享出去
*
* @param bitmap 位图
* @return
*/
public static Bitmap getShareBitmap(Context context, Bitmap bitmap, String nickName) {
//1.构建一个新的bitmap
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
//2.绘制原图
canvas.drawBitmap(bitmap, 0, 0, null);
//3.添加水印的画笔
Paint paint = new Paint();
paint.setDither(true);
//4.绘制水印图片
Bitmap shareBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.share_water_mark);
float top = bitmap.getHeight() - 10 - shareBitmap.getHeight();
canvas.drawBitmap(shareBitmap, 10, top, paint);
//5.绘制文字
String title = ResHelper.getString(R.string.comment_no_name);
if (!TextUtils.isEmpty(nickName)) {
title = nickName;
}
Paint textPaint = new Paint();
textPaint.setTextSize(16.0f);
textPaint.setTypeface(Typeface.createFromAsset(context.getAssets(), "watermark/font/01_fangzheng_lanting_xianhei.ttf"));
textPaint.setColor(Color.WHITE);
textPaint.setAntiAlias(true);
textPaint.setAlpha(200);
textPaint.setShadowLayer(1, .5f, .5f, 0x30000000);
if (!TextUtils.isEmpty(title)) {
title = "by:" + title;
int left = 10 + shareBitmap.getWidth() + 10;
top = top + 16;
canvas.drawText(title, left, top, textPaint);
}
shareBitmap.recycle();
bitmap.recycle();
//6.保存canvas状态
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return newBitmap;
}
public static void drawMeasureCount(Context context, View view, Canvas canvas,
Integer measureCount) {
final float density = context.getResources().getDisplayMetrics().density;
final float textSize = TEXT_SIZE_DP * density;
final float textOffset = TEXT_OFFSET_DP * density;
Paint textPaint = new Paint();
textPaint.setColor(0xFF888888);
textPaint.setTextSize(textSize);
textPaint.setShadowLayer(1, -1, 1, 0xFF000000);
String measureStr = "measure: " + measureCount + "x";
canvas.drawText(measureStr, textOffset, view.getHeight() - textOffset, textPaint);
}
/**
* Get rounded icon from the Bitmap object, with shade. The shade will only be drawn if
* the Bitmap is larger than the ImageView's size.
*
* @param resources Resources object for getting size and color.
* @param scaleBitmapImage Source Bitmap.
* @return Rounded BitmapDrawable with shade (if possible).
*/
public static Drawable getRoundedShape(Resources resources, Bitmap scaleBitmapImage) {
final int shadowSize = resources.getDimensionPixelSize(R.dimen.shadow_size);
final int shadowColor = resources.getColor(R.color.background_color);
int targetWidth = scaleBitmapImage.getWidth();
int targetHeight = scaleBitmapImage.getHeight();
Bitmap targetBitmap =
Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setDither(true);
Paint shadowPaint = new Paint(paint);
RectF rectF = new RectF(0, 0, targetWidth, targetHeight);
Canvas canvas = new Canvas(targetBitmap);
final BitmapShader shader =
new BitmapShader(scaleBitmapImage, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
paint.setShader(shader);
// Only apply shadow if the icon is large enough.
if (scaleBitmapImage.getWidth() >
resources.getDimensionPixelSize(R.dimen.notification_icon_size)) {
rectF.inset(shadowSize, shadowSize);
shadowPaint.setShadowLayer(shadowSize, 0f, 0f, shadowColor);
shadowPaint.setColor(Color.BLACK);
canvas.drawOval(rectF, shadowPaint);
}
canvas.drawOval(rectF, paint);
return new BitmapDrawable(resources, targetBitmap);
}
/**
* 绘制温度计形状
*
* @param shapePaint Paint
* @param canvas Canvas
*/
private void drawShapeBg(Paint shapePaint, Canvas canvas) {
shapePaint.setColor(thermometerBg);
shapePaint.setShadowLayer(8, 0, 0, thermometerShadowBg);
canvas.drawCircle(thermometerTopX, thermometerTopY, minThermometerRadius, shapePaint);
canvas.drawCircle(thermometerBottomX, thermometerBottomY, maxThermometerRadius, shapePaint);
canvas.drawRect(thermometerRectF, shapePaint);
/* 以下三句是为了去除部分不需要的阴影 */
shapePaint.clearShadowLayer();
canvas.drawCircle(thermometerTopX, thermometerTopY, minThermometerRadius, shapePaint);
canvas.drawCircle(thermometerBottomX, thermometerBottomY, maxThermometerRadius, shapePaint);
}
protected void init(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
final Resources res = getResources();
float density = context.getResources().getDisplayMetrics().density;
mArcColor = res.getColor(R.color.white);
if(attrs!=null){
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.DashedCircle, defStyle, 0);
mCircleWidth = (int) a.getDimension(
R.styleable.DashedCircle_circleWidth, mCircleWidth);
mArcColor = a.getColor(R.styleable.DashedCircle_circleColor, mArcColor);
a.recycle();
}
mCircleWidth = (int) (mCircleWidth * density);
mGlowStroke = (int) (mGlowStroke * density);
mShadowLayerStroke = (int) (mShadowLayerStroke * density);
mArcPaint = new Paint();
mArcPaint.setAntiAlias(true);
mArcPaint.setColor(Color.WHITE);
mArcPaint.setStyle(Paint.Style.STROKE);
mArcPaint.setStrokeWidth(mCircleWidth);
mGreyPaint = new Paint();
mGreyPaint.setAntiAlias(true);
mGreyPaint.setColor(getResources().getColor(R.color.white_with_10));
mGreyPaint.setStyle(Paint.Style.STROKE);
mGreyPaint.setStrokeWidth(mCircleWidth);
mRedPaint = new Paint();
mRedPaint.setAntiAlias(true);
mRedPaint.setColor(getResources().getColor(R.color.pink_banner));
mRedPaint.setStyle(Paint.Style.STROKE);
mRedPaint.setStrokeWidth(mCircleWidth);
mWhiteGlowPaint = new Paint();
mWhiteGlowPaint.setAntiAlias(true);
mWhiteGlowPaint.setColor(Color.WHITE);
mWhiteGlowPaint.setStyle(Paint.Style.STROKE);
mWhiteGlowPaint.setStrokeWidth(mCircleWidth);
mWhiteGlowPaint.setShadowLayer(mShadowLayerStroke, 0, 0, Color.WHITE);
setLayerType(LAYER_TYPE_SOFTWARE, mWhiteGlowPaint);
}
/**
* Build the final bitmap-pieces to draw in animation
*/
private void buildPieces(){
pieces = new Piece[pathArray.size()];
Paint paint = new Paint();
Matrix matrix = new Matrix();
Canvas canvas = new Canvas();
for(int i = 0; i < pieces.length; i++) {
int shadow = Utils.nextInt(Utils.dp2px(2),Utils.dp2px(9));
Path path = pathArray.get(i);
RectF r = new RectF();
path.computeBounds(r, true);
Bitmap pBitmap = Utils.createBitmapSafely((int)r.width() + shadow * 2,
(int)r.height() + shadow * 2, Bitmap.Config.ARGB_4444,1);
if(pBitmap == null){
pieces[i] = new Piece(-1, -1, null, shadow);
continue;
}
pieces[i] = new Piece((int)r.left + mTouchPoint.x - shadow,
(int)r.top + mTouchPoint.y - shadow, pBitmap, shadow);
canvas.setBitmap(pieces[i].bitmap);
BitmapShader mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
matrix.reset();
matrix.setTranslate(-r.left - offsetX + shadow, -r.top - offsetY + shadow);
mBitmapShader.setLocalMatrix(matrix);
paint.reset();
Path offsetPath = new Path();
offsetPath.addPath(path, -r.left + shadow, -r.top + shadow);
// Draw shadow
paint.setStyle(Paint.Style.FILL);
paint.setShadowLayer(shadow,0,0,0xff333333);
canvas.drawPath(offsetPath,paint);
paint.setShadowLayer(0,0,0,0);
// In case the view has alpha channel
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawPath(offsetPath,paint);
paint.setXfermode(null);
// Draw bitmap
paint.setShader(mBitmapShader);
paint.setAlpha(0xcc);
canvas.drawPath(offsetPath, paint);
}
// Sort by shadow
Arrays.sort(pieces);
}
/**
* Sets up {@link #mPaint} according to the props set on a shadow view. Returns {@code true}
* if the stroke should be drawn, {@code false} if not.
*/
protected boolean setupStrokePaint(Paint paint, float opacity) {
if (mStrokeWidth == 0 || mStrokeColor == null) {
return false;
}
paint.reset();
paint.setFlags(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
switch (mStrokeCap) {
case CAP_BUTT:
paint.setStrokeCap(Paint.Cap.BUTT);
break;
case CAP_SQUARE:
paint.setStrokeCap(Paint.Cap.SQUARE);
break;
case CAP_ROUND:
paint.setStrokeCap(Paint.Cap.ROUND);
break;
default:
throw new JSApplicationIllegalArgumentException(
"strokeCap " + mStrokeCap + " unrecognized");
}
switch (mStrokeJoin) {
case JOIN_MITER:
paint.setStrokeJoin(Paint.Join.MITER);
break;
case JOIN_BEVEL:
paint.setStrokeJoin(Paint.Join.BEVEL);
break;
case JOIN_ROUND:
paint.setStrokeJoin(Paint.Join.ROUND);
break;
default:
throw new JSApplicationIllegalArgumentException(
"strokeJoin " + mStrokeJoin + " unrecognized");
}
paint.setStrokeWidth(mStrokeWidth * mScale);
paint.setColor(Color.parseColor(mStrokeColor));
if (mStrokeDash != null && mStrokeDash.length > 0) {
paint.setPathEffect(new DashPathEffect(mStrokeDash, 0));
}
if (mShadowOpacity > 0) {
paint.setShadowLayer(mShadowRadius, mShadowOffsetX, mShadowOffsetY, mShadowColor);
}
return true;
}
private void initializeView(Context context, AttributeSet attrs) {
setScaleType(ScaleType.CENTER_INSIDE);
// http://www.google.com/design/spec/patterns/promoted-actions.html#promoted-actions-floating-action-button
final float density = getResources().getDisplayMetrics().density;
final TypedArray styles = context.obtainStyledAttributes(attrs, R.styleable.FabView, 0, 0);
mFabAttachTo = styles.getResourceId(R.styleable.FabView_fabuless_attachTo, 0);
mFabAttachAt = styles.getInt(R.styleable.FabView_fabuless_attachAt, TOP_RIGHT);
mFabAttachType = styles.getInt(R.styleable.FabView_fabuless_attachType, BORDER);
mFabSize = styles.getInt(R.styleable.FabView_fabuless_size, NORMAL);
mFabAttachPadding = (int) styles.getDimension(R.styleable.FabView_fabuless_padding, 16 * density);
mFabRevealAfterMs = styles.getInteger(R.styleable.FabView_fabuless_revealAfterMs, -1);
styles.recycle();
switch (mFabSize) {
case SMALL:
mShadowOffset = (int) (2 * density);
mFabRadius = (int) (20 * density);
break;
case NORMAL: default:
mShadowOffset = (int) (3 * density);
mFabRadius = (int) (28 * density);
break;
}
mBackgroundDrawable = new ShapeDrawable(new OvalShape());
final Paint paint = mBackgroundDrawable.getPaint();
paint.setShadowLayer(mShadowOffset, 0f, 0f * density, 0x60000000);
paint.setColor(mBackgroundColor);
setLayerType(LAYER_TYPE_SOFTWARE, paint);
mTouchSpotAnimator = new TouchSpotAnimator();
mFabRevealer = new FabRevealer();
if (mFabRevealAfterMs > -1 && getVisibility() == VISIBLE) {
setVisibility(INVISIBLE);
getViewTreeObserver().addOnPreDrawListener(mFabRevealer);
}
}
public GameRoundSummary(@NonNull Context context, @NonNull GameRound round, boolean rotate) {
this.round = round;
this.rotate = rotate;
blackPaint = new Paint();
blackPaint.setColor(Color.BLACK);
blackPaint.setAntiAlias(true);
blackPaint.setShadowLayer(SHADOW_RADIUS, SHADOW_DX, SHADOW_DY, SHADOW_COLOR);
blackTextPaint = new TextPaint();
blackTextPaint.setAntiAlias(true);
blackTextPaint.setColor(Color.BLACK);
FontsManager.set(context, blackTextPaint, R.font.roboto_medium);
whitePaint = new Paint();
whitePaint.setColor(Color.WHITE);
whitePaint.setAntiAlias(true);
whitePaint.setShadowLayer(SHADOW_RADIUS, SHADOW_DX, SHADOW_DY, SHADOW_COLOR);
whiteTextPaint = new TextPaint();
whiteTextPaint.setColor(Color.WHITE);
whiteTextPaint.setAntiAlias(true);
FontsManager.set(context, whiteTextPaint, R.font.roboto_medium);
winnerPaint = new Paint();
winnerPaint.setColor(WINNER_COLOR);
winnerPaint.setAntiAlias(true);
winnerPaint.setShadowLayer(SHADOW_RADIUS, SHADOW_DX, SHADOW_DY, SHADOW_COLOR);
grayTextPaint = new TextPaint();
grayTextPaint.setColor(Color.DKGRAY);
grayTextPaint.setAntiAlias(true);
FontsManager.set(context, grayTextPaint, R.font.roboto_medium);
backgroundPaint = new Paint();
backgroundPaint.setColor(BACKGROUND_COLOR);
boxPaint = new Paint();
boxPaint.setColor(Color.GRAY);
bitmap = Bitmap.createBitmap(measureWidth(), measureHeight(), Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
draw();
ThisApplication.sendAnalytics(Utils.ACTION_SHOW_ROUND);
}
private void init(AttributeSet attrs, int defStyleAttr, int defStyleRes) {
if (attrs == null)
return;
final Context context = getContext();
final Resources resource = getResources();
final float density = resource.getDisplayMetrics().density;
// Load attributes
final TypedArray a = context.obtainStyledAttributes(
attrs, R.styleable.FloatActionButton, defStyleAttr, defStyleRes);
ColorStateList bgColor = a.getColorStateList(R.styleable.FloatActionButton_gBackgroundColor);
int touchColor = a.getColor(R.styleable.FloatActionButton_gTouchColor, Ui.TOUCH_PRESS_COLOR);
boolean enabled = a.getBoolean(R.styleable.FloatActionButton_android_enabled, true);
int shadowColor = a.getInt(R.styleable.FloatActionButton_shadowColor, 0xFF000000);
float shadowDx = a.getDimension(R.styleable.FloatActionButton_shadowDx, density * Ui.X_OFFSET);
float shadowDy = a.getDimension(R.styleable.FloatActionButton_shadowDy, density * Ui.Y_OFFSET);
float shadowR = a.getDimension(R.styleable.FloatActionButton_shadowRadius, (density * Ui.SHADOW_RADIUS));
int shadowAlpha = a.getInt(R.styleable.FloatActionButton_shadowAlpha, 0x20);
float touchDurationRate = a.getFloat(R.styleable.FloatActionButton_gTouchDurationRate, 1.0f);
// Load intercept event type, the default is intercept click event
int interceptEvent = a.getInt(R.styleable.FloatActionButton_gInterceptEvent, 0x0001);
a.recycle();
// Enabled
setEnabled(enabled);
// BackgroundColor
if (bgColor == null) {
bgColor = UiCompat.getColorStateList(resource, R.color.g_default_float_action_bg);
}
// Background drawable
final float maxShadowOffset = Math.max(shadowDx, shadowDy);
mShadowRadius = (int) (shadowR + 0.5);
mShadowRadius += maxShadowOffset;
ShapeDrawable background = new ShapeDrawable(new OvalShadowShape(mShadowRadius,
Ui.changeColorAlpha(shadowColor, 0x70)));
// Background paint
Paint paint = background.getPaint();
if (!isInEditMode()) {
paint.setShadowLayer(mShadowRadius - maxShadowOffset, shadowDx, shadowDy,
Ui.changeColorAlpha(shadowColor, shadowAlpha));
}
// The background initial before setBackgroundColor
UiCompat.setBackground(this, background);
// Set the background color
setBackgroundColor(bgColor);
// TouchDrawable
mTouchDrawable = new TouchEffectDrawable(new FloatEffect(), ColorStateList.valueOf(touchColor));
mTouchDrawable.setCallback(this);
mTouchDrawable.setInterceptEvent(interceptEvent);
mTouchDrawable.setEnterDuration(touchDurationRate);
mTouchDrawable.setExitDuration(touchDurationRate);
// We want set this LayerType type on Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB
setLayerType(LAYER_TYPE_SOFTWARE, paint);
final int padding = mShadowRadius;
// set padding so the inner image sits correctly within the shadow.
setPadding(Math.max(padding, getPaddingLeft()),
Math.max(padding, getPaddingTop()),
Math.max(padding, getPaddingRight()),
Math.max(padding, getPaddingBottom()));
}
public CompassView(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
setFocusable(true);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.CompassView, defStyleAttr, 0);
if (a.hasValue(R.styleable.CompassView_bearing)) {
setBearing(a.getFloat(R.styleable.CompassView_bearing, 0));
}
a.recycle();
Context c = this.getContext();
Resources r = this.getResources();
circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
circlePaint.setColor(ContextCompat.getColor(c, R.color.background_color));
circlePaint.setStrokeWidth(1);
circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
circlePaint.setStyle(Paint.Style.STROKE);
northString = r.getString(R.string.cardinal_north);
eastString = r.getString(R.string.cardinal_east);
southString = r.getString(R.string.cardinal_south);
westString = r.getString(R.string.cardinal_west);
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(ContextCompat.getColor(c, R.color.text_color));
textPaint.setTextSize(40);
textPaint.setFakeBoldText(true);
textPaint.setSubpixelText(true);
textPaint.setTextAlign(Paint.Align.LEFT);
textPaint.setTextSize(30);
textHeight = (int)textPaint.measureText("yY");
markerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
markerPaint.setColor(ContextCompat.getColor(c, R.color.marker_color));
markerPaint.setAlpha(200);
markerPaint.setStrokeWidth(1);
markerPaint.setStyle(Paint.Style.STROKE);
markerPaint.setShadowLayer(2, 1, 1, ContextCompat.getColor(c,
R.color.shadow_color));
borderGradientColors = new int[4];
borderGradientPositions = new float[4];
borderGradientColors[3] = ContextCompat.getColor(c,
R.color.outer_border);
borderGradientColors[2] = ContextCompat.getColor(c,
R.color.inner_border_one);
borderGradientColors[1] = ContextCompat.getColor(c,
R.color.inner_border_two);
borderGradientColors[0] = ContextCompat.getColor(c,
R.color.inner_border);
borderGradientPositions[3] = 0.0f;
borderGradientPositions[2] = 1-0.03f;
borderGradientPositions[1] = 1-0.06f;
borderGradientPositions[0] = 1.0f;
glassGradientColors = new int[5];
glassGradientPositions = new float[5];
int glassColor = 245;
glassGradientColors[4] = Color.argb(65, glassColor,
glassColor, glassColor);
glassGradientColors[3] = Color.argb(100, glassColor,
glassColor, glassColor);
glassGradientColors[2] = Color.argb(50, glassColor,
glassColor, glassColor);
glassGradientColors[1] = Color.argb(0, glassColor,
glassColor, glassColor);
glassGradientColors[0] = Color.argb(0, glassColor,
glassColor, glassColor);
glassGradientPositions[4] = 1-0.0f;
glassGradientPositions[3] = 1-0.06f;
glassGradientPositions[2] = 1-0.10f;
glassGradientPositions[1] = 1-0.20f;
glassGradientPositions[0] = 1-1.0f;
skyHorizonColorFrom = ContextCompat.getColor(c,
R.color.horizon_sky_from);
skyHorizonColorTo = ContextCompat.getColor(c,
R.color.horizon_sky_to);
groundHorizonColorFrom = ContextCompat.getColor(c,
R.color.horizon_ground_from);
groundHorizonColorTo = ContextCompat.getColor(c,
R.color.horizon_ground_to);
}
public static Bitmap getBitmap(final String text, int fillColor, final String arrow, final int width, final int height, final int margin, final boolean strike_through, boolean expandable, final boolean shadow) {
{
if ((text == null) || (text.length() > 4)) return null;
try {
if ((width > 2000) || height > 2000 || height < 16 || width < 16) return null;
final Paint paint = new Paint();
paint.setStrikeThruText(strike_through);
paint.setStyle(Paint.Style.FILL);
paint.setColor(fillColor);
paint.setAntiAlias(true);
//paint.setTypeface(Typeface.MONOSPACE);
paint.setTypeface(Typeface.SANS_SERIF); // TODO BEST?
paint.setTextAlign(Paint.Align.LEFT);
float paintTs = (arrow == null ? 17 : 17 - arrow.length());
paint.setTextSize(paintTs);
final Rect bounds = new Rect();
final String fullText = text + (arrow != null ? arrow : "");
paint.getTextBounds(fullText, 0, fullText.length(), bounds);
float textsize = ((paintTs - 1) * (width - margin)) / bounds.width();
paint.setTextSize(textsize);
paint.getTextBounds(fullText, 0, fullText.length(), bounds);
// cannot be Config.ALPHA_8 as it doesn't work on Samsung
final Bitmap bitmap = Bitmap.createBitmap(width, expandable ? Math.max(height, bounds.height() + 30) : height, Bitmap.Config.ARGB_8888);
final Canvas c = new Canvas(bitmap);
if (shadow) {
paint.setShadowLayer(10, 0, 0, getCol(ColorCache.X.color_number_wall_shadow));
}
c.drawText(fullText, 0, (height / 2) + (bounds.height() / 2), paint);
return bitmap;
} catch (Exception e) {
if (JoH.ratelimit("icon-failure", 60)) {
UserError.Log.e(TAG, "Cannot create number icon: " + e);
}
return null;
}
}
}
private static Bitmap toRoundCornerShadowBitmap(Canvas canvas, Paint paintSolid, Paint paintSrcIn, Paint paintDstOut, Paint paintShadow, Bitmap srcBitmap, float[] roundRadius, float shadowSize, int shadowColor) {
int borderSize = XulUtils.roundToInt(shadowSize) * 2;
Bitmap output;
if (false) {
output = BitmapTools.createBitmapFromRecycledBitmaps(srcBitmap.getWidth(), srcBitmap.getHeight(), XulManager.DEF_PIXEL_FMT);
} else {
output = BitmapTools.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), XulManager.DEF_PIXEL_FMT);
}
canvas.setBitmap(output);
Rect rectSrc = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
Rect rectDst = new Rect(rectSrc);
XulUtils.offsetRect(rectDst, borderSize / 2, borderSize / 2);
RectF rectF = new RectF(rectDst);
if (roundRadius.length == 2) {
float rY = roundRadius[1];
float rX = roundRadius[0];
canvas.drawRoundRect(rectF, rX, rY, paintSolid);
canvas.drawBitmap(srcBitmap, rectSrc, rectDst, paintSrcIn);
XulUtils.saveCanvasLayer(canvas, 0, 0, output.getWidth(), output.getHeight(), paintSolid, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
paintShadow.setShadowLayer(shadowSize, 0, 0, shadowColor);
canvas.drawRoundRect(rectF, rX, rY, paintShadow);
canvas.drawRoundRect(rectF, rX, rY, paintDstOut);
XulUtils.restoreCanvas(canvas);
} else {
RoundRectShape roundRectShape = new RoundRectShape(roundRadius, null, null);
roundRectShape.resize(rectF.width(), rectF.height());
XulUtils.saveCanvas(canvas);
canvas.translate(rectF.left, rectF.top);
roundRectShape.draw(canvas, paintSolid);
canvas.translate(-rectF.left, -rectF.top);
canvas.drawBitmap(srcBitmap, rectSrc, rectDst, paintSrcIn);
XulUtils.saveCanvasLayer(canvas, 0, 0, output.getWidth(), output.getHeight(), paintSolid, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG);
paintShadow.setShadowLayer(shadowSize, 0, 0, shadowColor);
canvas.translate(rectF.left, rectF.top);
roundRectShape.draw(canvas, paintShadow);
roundRectShape.draw(canvas, paintDstOut);
XulUtils.restoreCanvas(canvas);
XulUtils.restoreCanvas(canvas);
}
canvas.setBitmap(null);
return output;
}