Android circular border around circular image - android

I do this to get a circular ImageView, how can I add a circle border around it like this picture:
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage,int width) {
int targetWidth = width;
int targetHeight = width;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth,
targetHeight,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addCircle(((float) targetWidth - 1) / 2,
((float) targetHeight - 1) / 2,
(Math.min(((float) targetWidth),
((float) targetHeight)) / 2),
Path.Direction.CCW);
canvas.clipPath(path);
Bitmap sourceBitmap = scaleBitmapImage;
canvas.drawBitmap(sourceBitmap,
new Rect(0, 0, sourceBitmap.getWidth(),
sourceBitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), null);
return targetBitmap;
}

There is a very interesting post about this feature:
http://www.curious-creature.org/2012/12/11/android-recipe-1-image-with-rounded-corners/
It is written by Romain Guy (ex android team at Google).
You can use something like this:
This example create a circular bitmap, with around a white stroke.
You can change it, adding a white stroke with a black line.
public class CircleDrawable extends Drawable {
private final BitmapShader mBitmapShader;
private final Paint mPaint;
private Paint mWhitePaint;
int circleCenterX;
int circleCenterY;
int mRadus;
private boolean mUseStroke = false;
private int mStrokePadding = 0;
public CircleDrawable(Bitmap bitmap) {
mBitmapShader = new BitmapShader(bitmap,
Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setShader(mBitmapShader);
}
public CircleDrawable(Bitmap bitmap, boolean mUseStroke) {
this(bitmap);
if (mUseStroke) {
this.mUseStroke = true;
mStrokePadding = 4;
mWhitePaint = new Paint();
mWhitePaint.setStyle(Paint.Style.FILL_AND_STROKE);
mWhitePaint.setStrokeWidth(0.75f);
mWhitePaint.setColor(Color.WHITE);
}
}
#Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
circleCenterX = bounds.width() / 2;
circleCenterY = bounds.height() / 2;
if (bounds.width() >= bounds.height())
mRadus = bounds.width() / 2;
else
mRadus = bounds.height() / 2;
}
#Override
public void draw(Canvas canvas) {
if (mUseStroke) {
canvas.drawCircle(circleCenterX, circleCenterY, mRadus, mWhitePaint);
}
canvas.drawCircle(circleCenterX, circleCenterY, mRadus - mStrokePadding, mPaint);
}
#Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
#Override
public void setAlpha(int alpha) {
mPaint.setAlpha(alpha);
}
#Override
public void setColorFilter(ColorFilter cf) {
mPaint.setColorFilter(cf);
}
public boolean ismUseStroke() {
return mUseStroke;
}
public void setmUseStroke(boolean mUseStroke) {
this.mUseStroke = mUseStroke;
}
}

Related

Custom form of progress bar

I want to create app in which will be free-form progressbar like this:
Tell me please how can this be done? Perhaps some kind of example.
Thanks.
you can start from this simple custom view (the main idea is to use PathMeasure#getSegment() method):
class V extends View implements View.OnClickListener {
Path segment = new Path();
Paint paint = new Paint();
PathMeasure pm;
public V(Context context) {
super(context);
setOnClickListener(this);
paint.setColor(0xaa00ff00);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(48);
paint.setStrokeCap(Paint.Cap.ROUND);
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Path src = new Path();
src.moveTo(16.000000f, 1.630000f);
src.lineTo(22.030001f, 1.630000f);
src.cubicTo(23.420000f, 1.620000f, 24.770000f, 2.390000f, 25.469999f, 3.590000f);
src.lineTo(31.469999f, 14.030000f);
src.cubicTo(32.169998f, 15.240000f, 32.169998f, 16.790001f, 31.469999f, 18.000000f);
src.lineTo(25.440001f, 28.440001f);
src.cubicTo(24.740000f, 29.629999f, 23.379999f, 30.379999f, 22.000000f, 30.379999f);
src.lineTo(9.970000f, 30.379999f);
src.cubicTo(8.580000f, 30.379999f, 7.230000f, 29.610001f, 6.530000f, 28.410000f);
src.lineTo(0.530000f, 17.969999f);
src.cubicTo(-0.170000f, 16.760000f, -0.170000f, 15.210000f, 0.530000f, 14.000000f);
src.lineTo(6.560000f, 3.560000f);
src.cubicTo(7.260000f, 2.370000f, 8.620000f, 1.620000f, 10.000000f, 1.630000f);
src.close();
Matrix m = new Matrix();
RectF srcRect = new RectF(0, 0, 32, 32);
RectF dstRect = new RectF(0, 0, w, h);
dstRect.inset(paint.getStrokeWidth() / 2, paint.getStrokeWidth() / 2);
m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER);
Path dst = new Path();
src.transform(m, dst);
pm = new PathMeasure(dst, true);
pm.getSegment(0, pm.getLength() / 2, segment, true);
segment.rLineTo(0, 0);
}
void setProgress(float progress) {
segment.reset();
float length = pm.getLength();
float start = progress % length;
float end = (progress + length / 2) % length;
if (start < end) {
pm.getSegment(start, end, segment, true);
} else {
pm.getSegment(start, length, segment, true);
pm.getSegment(0, end, segment, true);
}
segment.rLineTo(0, 0);
invalidate();
}
#Override
public void onClick(View v) {
ObjectAnimator.ofFloat(this, "progress", 0, 10 * pm.getLength()).setDuration(10 * 2500).start();
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(segment, paint);
}
}
~

Custom bitmap transformation method with Picasso

I have a function which takes in a bitmap as a parameter, and return a bitmap.
public Bitmap setRoundedCornes(Bitmap b, int l, int r, int t, int b)
Before using Picasso, I used this method before my final bitmap was used in the app.
Now I'm using Picasso and am unsure how to apply this method.
Does anyone have any idea?
EDIT
I now have:
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int topLeftX, int topLeftY, int topRightX, int topRightY, int bottomRightX, int bottomRightY, int bottomLeftX, int bottomLeftY) {
try {
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
// the float array passed to this function defines the x/y values of the corners
// it starts top-left and works clockwise
// so top-left-x, top-left-y, top-right-x etc
RoundRectShape rrs = new RoundRectShape(new float[]{topLeftX, topLeftY, topRightX, topRightY, bottomRightX, bottomRightY, bottomLeftX, bottomLeftY}, null, null);
canvas.drawARGB(0, 0, 0, 0);
paint.setAntiAlias(true);
paint.setColor(0xFF000000);
rrs.resize(bitmap.getWidth(), bitmap.getHeight());
rrs.draw(canvas, paint);
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}catch(Exception e){
return bitmap;
}
}
and
public class MyTransform implements Transformation {
#Override
public Bitmap transform(Bitmap source) {
//your logic to transform goes here
return getRoundedCornerBitmap(source, 20, 20, 20, 20, 0, 0, 0, 0);
}
#Override
public String key() {
return "circle";
}
}
Create a class for transformation which implements Transformation
for e.g.
public class MyTransform implements Transformation {
#Override
public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
if (squaredBitmap != source) {
source.recycle();
}
Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
BitmapShader shader = new BitmapShader(squaredBitmap,
BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
paint.setShader(shader);
paint.setAntiAlias(true);
float r = size / 2f;
canvas.drawCircle(r, r, r, paint);
squaredBitmap.recycle();
return bitmap;
}
#Override
public String key() {
return "circle";
}
}
now to apply this transformation you can use picasso like this
Picasso.with(getContext())
.load(url)
.transform(new MyTransform())
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
// do something if its loaded successfully
}
#Override
public void onError() {
// do something if its not loaded successfully
}
});
Yes you can apply Transformation like this one:
public class CropSquareTransformation implements Transformation {
#Override public Bitmap transform(Bitmap source) {
int size = Math.min(source.getWidth(), source.getHeight());
int x = (source.getWidth() - size) / 2;
int y = (source.getHeight() - size) / 2;
Bitmap result = Bitmap.createBitmap(source, x, y, size, size);
if (result != source) {
source.recycle();
}
return result;
}
#Override public String key() { return "square()"; }
}
See Picasso documentation

How crop bitmap of selected area on canvas?

I am making an application I am unable to one issue last 3 days doing google as much possible.I make a circle on canvas and want to crop image that part and show that image in zoom mode.My first step is like in screen here:-
in this I am selecting area.here is my code used by me for this.
private float x, y;
private boolean zooming = false;
private Paint mPaint;
private Matrix mmatrix;
private Shader mShader;
private Bitmap mBitmap;
private List<Point> mpoints;
private List<MyPoints> mpointlist;
private Path mpath;
private Canvas mcanvas;
private Bitmap mresult_bitmap, resultingImage,finalbitmap;
private Context mcontext;
private boolean bfirstpoint = false;
private Point mfirstpoint = null;
private Point mlastpoint = null;
public CircularZoomView(Context context) {
super(context);
mcontext = context;
mpath = new Path();
mpoints = new ArrayList<Point>();
setBackgroundResource(R.drawable.testing);
mPaint = new Paint();
mresult_bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.testing);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.RED);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (zooming && resultingImage!=null) {
zooming = false;
ShowImage(resultingImage);
canvas.drawBitmap(resultingImage,mmatrix, null);
}
boolean first = true;
for (int i = 0; i < mpoints.size(); i += 2) {
Point point = mpoints.get(i);
if (first) {
first = false;
mpath.moveTo(point.x, point.y);
} else if (i < mpoints.size() - 1) {
Point next = mpoints.get(i + 1);
mpath.quadTo(point.x, point.y, next.x, next.y);
} else {
mlastpoint = mpoints.get(i);
mpath.lineTo(point.x, point.y);
}
}
canvas.drawPath(mpath, mPaint);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
x = event.getX();
y = event.getY();
Point point = new Point();
point.x = (int) event.getX();
point.y = (int) event.getY();
if (bfirstpoint) {
if (comparepoint(mfirstpoint, point)) {
mpoints.add(mfirstpoint);
addCircleFromPath(mpath);
} else {
mpoints.add(point);
}
} else {
mpoints.add(point);
}
if (!(bfirstpoint)) {
mfirstpoint = point;
bfirstpoint = true;
}
invalidate();
switch (action) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
zooming = false;
this.invalidate();
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
zooming = true;
mlastpoint = point;
if (mpoints.size() > 12) {
if (!comparepoint(mfirstpoint, mlastpoint)) {
mpoints.add(mfirstpoint);
addCircleFromPath(mpath);
}
}
this.invalidate();
break;
default:
break;
}
return true;
}
public Bitmap getCroppedBitmap(Bitmap bitmap) {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
void ShowImage(Bitmap mbitmap) {
Display display = ((MainActivity) mcontext).getWindowManager().getDefaultDisplay();
int screenWidth = display.getWidth();
float imageWidth = (float)mbitmap.getWidth();
float imageHeight = (float)mbitmap.getHeight();
float newHeight = imageHeight / (imageWidth / screenWidth);
float newWidth = screenWidth;
float scaleWidth = screenWidth / imageWidth;
float scaleHeight = newHeight / imageHeight;
SetImageMatrix(mbitmap,scaleWidth,scaleHeight);
}
void SetImageMatrix(Bitmap image,float scaleWidth, float scaleHeight) {
mmatrix = new Matrix();
mmatrix.setTranslate(40,40);
mmatrix.postScale(scaleWidth/2, scaleHeight/2);
/*image.setImageMatrix(mmatrix);
image.setScaleType(ScaleType.MATRIX);
image.invalidate();*/
}
private boolean comparepoint(Point first, Point current) {
int left_range_x = (int) (current.x - 3);
int left_range_y = (int) (current.y - 3);
int right_range_x = (int) (current.x + 3);
int right_range_y = (int) (current.y + 3);
if ((left_range_x < first.x && first.x < right_range_x)
&& (left_range_y < first.y && first.y < right_range_y)) {
if (mpoints.size() < 10) {
return false;
} else {
return true;
}
} else {
return false;
}
}
private void addCircleFromPath(Path path){
RectF bounds = new RectF();
path.computeBounds(bounds, true);
int width = (int) (bounds.right-bounds.left);
int height = (int) (bounds.bottom-bounds.top);
if(width<20 && height<20){
path.reset();
return;
}
int radius ;
if(width>=height)
radius = Math.round(((width/2)));
else radius = Math.round((int) ((height/2)));
/*CircleTagObject circle = new CircleTagObject((int)bounds.left+width/2, (int)bounds.top+height/2, radius, crossBitmap, tagBitmap,circleArray.size(),
ImageEditorView.this);
circleArray.add(circle);
tagBallID = circleArray.size() - 1;
dragEnable = true;*/
resultingImage = getCroppedBitmap(Bitmap.createBitmap(mresult_bitmap,0,0,200,200));
mcanvas = new Canvas(resultingImage);
path.reset();
resetView();
invalidate();
}
public void resetView() {
mpoints.clear();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setPathEffect(new DashPathEffect(new float[] { 10, 20 }, 0));
mPaint.setStrokeWidth(5);
mPaint.setColor(Color.RED);
invalidate();
}
If I create hard coded bitmap like above its showing good but not crop bitmap of selected part.Like this image.
but when add exact coordinate of selected area Like as:-
resultingImage = getCroppedBitmap(Bitmap.createBitmap(mresult_bitmap,(int)bounds.left,(int)bounds.top,width,height));
Then exception occure:-
07-12 10:58:56.700: E/MessageQueue-JNI(12310): java.lang.IllegalArgumentException: y + height must be <= bitmap.height()
07-12 10:58:56.700: E/MessageQueue-JNI(12310): at android.graphics.Bitmap.createBitmap(Bitmap.java:565)
07-12 10:58:56.700: E/MessageQueue-JNI(12310): at android.graphics.Bitmap.createBitmap(Bitmap.java:530)
07-12 10:58:56.700: E/MessageQueue-JNI(12310): at com.intel.view.CircularZoomView.addCircleFromPath(CircularZoomView.java:237)
I know why This exception occurs but unable to find solution how crop image of selected part.Thanks in advance.
I know its too late for your solution but this may help to others Use of this code
help you to come out from this problem.

how to rotate image with background color?

I am trying to rotate image to some degree and fill background to red color. For example I have a image and want to rotate minute niddle to 15 min(45 degree) and show that quarter circle with red color.
I used following code with rotate my rotate to some angle but i am not able to fill background color. Please help.
RotateAnimation rotateAnimation = new RotateAnimation(
(1 - 1) * 6, 10 * 6,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setDuration(5000);
rotateAnimation.setFillEnabled(true);
rotateAnimation.setFillBefore(true);
rotateAnimation.setBackgroundColor(Color.RED);
orgClockImage.startAnimation(rotateAnimation);
Thanks
you can rotate the bitmap with matrix also.
public class RotateBitmap {
public static final String TAG = "RotateBitmap";
private Bitmap mBitmap;
private int mRotation;
private int mWidth;
private int mHeight;
private int mBitmapWidth;
private int mBitmapHeight;
public RotateBitmap( Bitmap bitmap, int rotation )
{
mRotation = rotation % 360;
setBitmap( bitmap );
}
public void setRotation( int rotation )
{
mRotation = rotation;
invalidate();
}
public int getRotation()
{
return mRotation % 360;
}
public Bitmap getBitmap()
{
return mBitmap;
}
public void setBitmap( Bitmap bitmap )
{
mBitmap = bitmap;
if ( mBitmap != null ) {
mBitmapWidth = bitmap.getWidth();
mBitmapHeight = bitmap.getHeight();
invalidate();
}
}
private void invalidate()
{
Matrix matrix = new Matrix();
int cx = mBitmapWidth / 2;
int cy = mBitmapHeight / 2;
matrix.preTranslate( -cx, -cy );
matrix.postRotate( mRotation );
matrix.postTranslate( cx, cx );
RectF rect = new RectF( 0, 0, mBitmapWidth, mBitmapHeight );
matrix.mapRect( rect );
mWidth = (int)rect.width();
mHeight = (int)rect.height();
}
public Matrix getRotateMatrix()
{
Matrix matrix = new Matrix();
if ( mRotation != 0 ) {
int cx = mBitmapWidth / 2;
int cy = mBitmapHeight / 2;
matrix.preTranslate( -cx, -cy );
matrix.postRotate( mRotation );
matrix.postTranslate( mWidth / 2, mHeight / 2 );
}
return matrix;
}
public int getHeight()
{
return mHeight;
}
public int getWidth()
{
return mWidth;
}
public void recycle()
{
if ( mBitmap != null ) {
mBitmap.recycle();
mBitmap = null;
}
}
}

Why does resize make my image look strange?

I have a relative layout on which i add a view. this is the code in the activity:
paint = (RelativeLayout) findViewById(R.id.paint);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
myView = new CustomImage(getBaseContext());
paint.addView(myView);
String imagePath = (String) getIntent().getExtras().get("selectedImagePath");
LogService.log("PaintActivity", "Imagepath: " + imagePath);
// Drawable background = BitmapDrawable.createFromPath(imagePath);
// bitmap = ((BitmapDrawable) background).getBitmap();
try {
fis = new FileInputStream(imagePath);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mBitmap = loadBitmap(fis.getFD());
bitmap = getResizedBitmap(mBitmap, 412*2, 1024*2);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
LogService.log("PaintActivity", "Bitmap = " + mBitmap);
myView.setBitmap(bitmap);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
mPaint.setColor(0xFF000000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(50);
myView.setPaint(mPaint);
}
}, 50);
This is my View:
public class CustomImage extends View {
private boolean hasResized = false;
private Bitmap bitmap, bitmap2;
public Context context;
Paint paint;
private Canvas canvas;
public String text = null;
private Paint mBitmapPaint, paintText;
private Path mPath = new Path();
private float mX, mY, pX, pY, tX, tY;
private static final float TOUCH_TOLERANCE = 4;
private float screenDensity;
int height, width;
private Bitmap mBitmap;
private RandomAccessFile randomAccessFile;
private int bh;
private int bw;
public CustomImage(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public CustomImage(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomImage(Context context) {
super(context);
init(context);
}
private void init(Context context) {
this.context = context;
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
}
public void setImg(Context context, Canvas canvas, Bitmap bitmapw, int width, int height) {
bw = bitmapw.getWidth();
bh = bitmapw.getHeight();
float scaleWidth = ((float) width)/ bw;
float scaleHeight = ((float) height)/ bh;
System.out.println("CustomImage.setImg()");
bitmap = bitmapw;
}
public void setPaint(Paint paint) {
this.paint = paint;
LogService.log("in setPaint", "paint = " + paint);
}
public void setBitmap(Bitmap bitmap) throws Exception {
File file = new File("/mnt/sdcard/sample/temp.txt");
file.getParentFile().mkdirs();
randomAccessFile = new RandomAccessFile(file, "rw");
int bWidth = bitmap.getWidth();
int bHeight = bitmap.getHeight();
FileChannel channel = randomAccessFile.getChannel();
MappedByteBuffer map = channel.map(MapMode.READ_WRITE, 0, bWidth*bHeight*4);
bitmap.copyPixelsToBuffer(map);
bitmap.recycle();
this.bitmap = Bitmap.createBitmap(bWidth, bHeight, Config.ARGB_8888);
map.position(0);
this.bitmap.copyPixelsFromBuffer(map);
channel.close();
randomAccessFile.close();
// this.bitmap = bitmap;
// canvas = new Canvas();
}
public void setBitmap2(Bitmap bitmap) {
bitmap2 = bitmap;
invalidate();
}
public void getText(String text) {
this.text = text;
paintText = new Paint();
paintText.setColor(0xFFFFFFFF);
paintText.setStrokeWidth(10);
paintText.setTextSize(20);
invalidate();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, 0, 0, mBitmapPaint);
if (PaintActivity.isPic == 1) {
if (bitmap2 != null) {
canvas.drawBitmap(bitmap2, mX, mY, mBitmapPaint);
pX = mX;
pY = mY;
}
if(text != null){
canvas.drawText(text, tX, tY, paintText);
}
} else if (PaintActivity.isPic == 2) {
if(text != null){
canvas.drawText(text, mX, mY, paintText);
tX = mX;
tY = mY;
}
if (bitmap2 != null) {
canvas.drawBitmap(bitmap2, pX, pY, mBitmapPaint);
}
}else{
canvas.drawPath(mPath, paint);
if (bitmap2 != null) {
canvas.drawBitmap(bitmap2, pX, pY, mBitmapPaint);
}
if(text != null){
canvas.drawText(text, tX, tY, paintText);
}
}
}
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
canvas.drawPoint(x, y, paint);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if ((dx >= TOUCH_TOLERANCE) || (dy >= TOUCH_TOLERANCE)) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.lineTo(mX, mY);
mPath.moveTo(mX, mY);
canvas.drawPath(mPath, paint);
mPath.reset();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
System.out.println("CustomImage.onSizeChanged()");
super.onSizeChanged(w, h, oldw, oldh);
if ((w != 0) && (h != 0)) {
if (!hasResized) {
hasResized = true;
renderImage();
}
}
}
public void renderImage() {
System.out.println("======in renderimage=======w " + getMeasuredWidth() + "h " + getMeasuredHeight());
width = getMeasuredWidth();
height = getMeasuredHeight();
canvas = new Canvas(bitmap);
setImg(context, canvas, bitmap, width, height);
}
}
As you can see, I have a resizing function in the activity:
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
Now if i comment this function out of the code, the image looks normal, but not scaled. If i leave it there, then i get an ugly image, but it is resized. Example:
normal not resized: http://imgur.com/BD5Pp,YWDTi
altered but resized pic: http://imgur.com/BD5Pp,YWDTi#1
As OP stated answering my question, the bitmap is converted to RGB_565, which is a known bug in createBitmap() - #16211, fixed in Android 3.0. It affects createScaledBitmap() as well. The bitmap wouldn't look distorted if it remained ARGB_8888.
As a workaround you have to create target bitmap manually (which gives you full control of the pixel format), then create canvas attached to this bitmap and paint a scaled version of the original bitmap on it. Here's a sample from my project:
public class BitmapUtils {
private static Matrix matrix = new Matrix();
private static Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
public static final Bitmap resizeBitmap(Bitmap bitmap, float scale, Bitmap.Config targetConfig) {
int srcWidth = bitmap.getWidth();
int srcHeight = bitmap.getHeight();
int newWidth = (int) (srcWidth * scale);
int newHeight = (int) (srcHeight * scale);
float sx = newWidth / (float) srcWidth;
float sy = newHeight / (float) srcHeight;
matrix.setScale(sx, sy);
Bitmap target = Bitmap.createBitmap(newWidth, newHeight, targetConfig);
Canvas c = new Canvas(target);
c.drawBitmap(bitmap, matrix, paint);
return target;
}
}
Note: this code is not reentrant, due to global matrix instance. Also, it takes scale as parameter, but adapting it to your needs (new width/height as parameters) is trivial.

Categories

Resources