Color Picker: use oval preview

this code was in old light customization settings commit, no idea who made it

Signed-off-by: xyyx <xyyx@mail.ru>
This commit is contained in:
ezio84
2017-09-18 10:49:47 +02:00
committed by hungphan2001
parent 82cfe1a62e
commit 301473dca2
2 changed files with 34 additions and 22 deletions

View File

@@ -23,6 +23,8 @@ import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
@@ -200,8 +202,10 @@ public class ColorPickerPreference extends Preference implements
}
widgetFrameView.addView(iView);
widgetFrameView.setMinimumWidth(0);
iView.setBackgroundDrawable(new AlphaPatternDrawable((int) (5 * mDensity)));
iView.setImageBitmap(getPreviewBitmap());
final int size = (int) getContext().getResources().getDimension(R.dimen.picker_circle_preview_size);
final int imageColor = ((mValue & 0xF0F0F0) == 0xF0F0F0) ?
(mValue - 0x101010) : mValue;
iView.setImageDrawable(createOvalShape(size, 0xFF000000 + imageColor));
iView.setTag("preview");
iView.setOnClickListener(new View.OnClickListener() {
@Override
@@ -221,26 +225,6 @@ public class ColorPickerPreference extends Preference implements
}
}
private Bitmap getPreviewBitmap() {
int d = (int) (mDensity * 31); // 30dip
int color = mValue;
Bitmap bm = Bitmap.createBitmap(d, d, Config.ARGB_8888);
int w = bm.getWidth();
int h = bm.getHeight();
int c = color;
for (int i = 0; i < w; i++) {
for (int j = i; j < h; j++) {
c = (i <= 1 || j <= 1 || i >= w - 2 || j >= h - 2) ? Color.GRAY : color;
bm.setPixel(i, j, c);
if (i != j) {
bm.setPixel(j, i, c);
}
}
}
return bm;
}
@Override
public void onColorChanged(int color) {
if (isPersistent()) {
@@ -414,4 +398,12 @@ public class ColorPickerPreference extends Preference implements
}
};
}
private static ShapeDrawable createOvalShape(int size, int color) {
ShapeDrawable shape = new ShapeDrawable(new OvalShape());
shape.setIntrinsicHeight(size);
shape.setIntrinsicWidth(size);
shape.getPaint().setColor(color);
return shape;
}
}