Animation using Runnable? - android

I'm trying to animate an ImageView using Runnable and Handler.postDelayed. Bitmap operations all work fine but instead of the animation I get the final result of the ImageView. What is wrong in here?
final ImageView imageView = (ImageView) findViewById(R.id.imageView);
final Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.clock_flip_top);
final Matrix matrix = new Matrix();
for (i = 0; i < 30; i++) {
float c = i/5;
src = new float[] { 0, 0, bitmap.getWidth(), 0,
bitmap.getWidth(), bitmap.getHeight(), 0,
bitmap.getHeight() };
dst = new float[] { 0 - c, 0, bitmap.getWidth() + c, 0,
bitmap.getWidth(), bitmap.getHeight(), 0,
bitmap.getHeight() };
anim = new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
matrix.setPolyToPoly(src, 0, dst, 0, src.length >> 1);
Bitmap newBitmap = Bitmap
.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
bitmap.getHeight(), matrix, true);
imageView.setImageBitmap(newBitmap);
handler.postDelayed(anim, i * 200);
}
};
anim.run();
}

You do not call run(). To have your Runnable object run in another thread you must create a Thread object passing it your Runnable object in the constructor to Thread object. You then call start() on the thread object. See here for detailed description of how to start threads.

Related

add text over a barcode

I'm trying to add a text over a barcode.
In the final stage I want to make the barcode height smaller, and put the text below the barcode, but I'm stuck to add the text.
I tried to play with the X, Y beginning, but no luck.
What am I missing?
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
try {
BitMatrix bitMatrix = multiFormatWriter.encode(finalLastID, BarcodeFormat.CODE_128, imageView.getWidth(), imageView.getHeight());
Bitmap bitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.RGB_565);
for (int i = 0; i < imageView.getWidth(); i++){
for (int j = 0; j < imageView.getHeight()-scriptTextHeight; j++){
bitmap.setPixel(i,j,bitMatrix.get(i,j)? Color.BLACK:Color.WHITE);
}
}
drawStringonBitmap(bitmap,"TEST", 0, 0, Color.RED, 60, 20, false, imageView.getWidth(), imageView.getHeight());
imageView.setImageBitmap(bitmap);
} catch (WriterException e) {
e.printStackTrace();
}
public static Bitmap drawStringonBitmap(Bitmap src, String string, int beginX, int beginY, int color, int alpha, int size, boolean underline, int width , int height) {
Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
paint.setColor(color);
paint.setAlpha(alpha);
paint.setTextSize(size);
paint.setAntiAlias(true);
paint.setUnderlineText(underline);
canvas.drawBitmap(src, 0, 0, null);
canvas.drawText(string, beginX, beginY, paint);
return result;
}
Seems like a flaw in your code flow, you are using your drawStringOnBitmap() method like a void method, because you are doing nothing with the returned Bitmap, so I guess your code should be sth like :
bitmap = drawStringonBitmap(bitmap,"TEST", 0, 0, Color.RED, 60, 20, false, imageView.getWidth(), imageView.getHeight());
at that point.
Can't test it but I am guessing then :
imageView.setImageBitmap(bitmap);
should set the right bitmap.
Create a view of barcode and text below it and take that view as a bitmap just like a screenshot.
public Bitmap getBitmapFromView(View view) {
view.setDrawingCacheEnabled(true);
Bitmap returnedBitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
Bitmap.createScaledBitmap(returnedBitmap, img.getWidth(), img.getHeight(), false);
return returnedBitmap;
}

Rotate custom view in android

I am trying to rotate an image. Rotation is perfectly working but when I get that rotated image back getting some blank space. What should I do for remove that blank space?
I have tried this code:
public static Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.setRotate(90, 0, 0);
matrix.postTranslate(source.getHeight(), 0);
// matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Utility.tempBitmap = RotateBitmap(Utility.tempBitmap, -90);
I solved my problem by changing in view set bitmap some changes as per my requirement. using this code :
the code i used before (Gives me white space)
public static Bitmap RotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.setRotate(90, 0, 0);
matrix.postTranslate(source.getHeight(), 0);
// matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
Utility.tempBitmap = RotateBitmap(Utility.tempBitmap, -90);
Solution code is here:
public int setBitmap(final Bitmap bitmap)
{
changeBitmapContrastBrightness(1, 50);
gapRect = new RectF();
dest = new Rect();
setBackgroundColor(Color.GRAY);
if (bitmap.getWidth() > bitmap.getHeight()) {
this.mbitmap = bitmap;
invalidate();
return 0;
}
this.mbitmap = bitmap;
invalidate();
return 1;

Ovelay 2 images

I am trying to overlay 2 images on on top of other.
One image i getting from other class:
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
The other image is from drawable:
Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),R.drawable.topshow);
Then I get the overlay:
Bitmap overLay = (overlay(bitmap,icon));
Overlay func:
public static Bitmap overlay(Bitmap bmp1, Bitmap bmp2)
{
Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
Canvas canvas = new Canvas(bmOverlay);
canvas.drawBitmap(bmp1, new Matrix(), null);
canvas.drawBitmap(bmp2, 0, 0, null);
return bmOverlay;
}
And save the overlay as jpg:
overLay.compress(Bitmap.CompressFormat.JPEG, 80,new FileOutputStream(file_name + ".jpg"));
The problem is the image I get look not so good. On the border of the overlay image is interference and it looks like low quality.
Both images are 288x384. They both look good before the overlay. What can you suggest me?
Try this code. I hope it will help you.
Bitmap bMap = null;
Bitmap tempbMap = null;
tempbMap = Constants.wholeBitmap;
bMap = Bitmap.createScaledBitmap(tempbMap, 320, 320, true);
int BORDER_WIDTH = 0;
int BORDER_COLOR = Color.parseColor("#00000000");
Bitmap res = Bitmap.createBitmap(bMap.getWidth() + 2 * BORDER_WIDTH,
bMap.getHeight() + 2 * BORDER_WIDTH,
bMap.getConfig());
Canvas c = new Canvas(res);
Paint p = new Paint();
p.setColor(BORDER_COLOR);
c.drawRect(0, 0, res.getWidth(), res.getHeight(), p);
p = new Paint(Paint.FILTER_BITMAP_FLAG);
c.drawBitmap(bMap, BORDER_WIDTH, BORDER_WIDTH, p);
Bitmap bMapFinal = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), null, true);
Bitmap bitmapOverlay = BitmapFactory.decodeResource(getResources(), R.drawable.overlay_3_large);
int width = 293;
int height = 55;
int w = (int) (bMapFinal.getWidth()/2);
int h = (int) ((w * height) / width);
Bitmap scaled = Bitmap.createScaledBitmap(bitmapOverlay, w, h, true);
c.drawBitmap(scaled, bMapFinal.getWidth() - scaled.getWidth(), bMapFinal.getHeight() - scaled.getHeight(), p);
Bitmap bMapFinal_new = Bitmap.createBitmap(res, 0, 0, res.getWidth(), res.getHeight(), null, true);
ivImageMain.setImageBitmap(null);
ivImageMain.destroyDrawingCache();
Constants.finalBitmapForShare = bMapFinal_new;
ivImageMain.setImageBitmap(bMapFinal_new);
Thank you.

Set a matrix on bitmap

Hy,
I want set only matrix in this code but if i use this statemant , it reset all drawing picture.
Bitmap a= Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
a need only new matrix but how i get it
I want only:
public static Bitmap flip(Bitmap src, int type) {
// create new matrix for transformation
Matrix matrix = new Matrix();
// if vertical
if(type == FLIP_VERTICAL) {
// y = y * -1
matrix.preScale(1.0f, -1.0f);
}
// if horizonal
else if(type == FLIP_HORIZONTAL) {
// x = x * -1
matrix.preScale(-1.0f, 1.0f);
// unknown type
} else {
return null;
}
// return transformed image
//Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
Bitmap pp= Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
return pp;
}
If oyu saw this Bitmao pp= ...
I want only change src ->matrix and that is all

Show marker inside a resource drawable which obtained from url

Here is the map page I have, which shows all the users of my App. Also the images(markers) are obtained from the URL given from my server. These markers has to put inside a Drawable(a circle like image as shown). I created a circle like Bitmap from the url using Canvas.
public Drawable showMe(String url)
{
Bitmap bitmap=null;
try {
URL newurl = new URL(url);
bitmap = BitmapFactory.decodeStream(newurl.openConnection().getInputStream());
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap=getBitmap(url);
Paint paint = new Paint();
paint.setFilterBitmap(true);
int targetWidth = 30;
int targetHeight = 30;
Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight,Bitmap.Config.ARGB_8888);
RectF rectf = new RectF(0, 0, 30, 30);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addRoundRect(rectf, targetWidth, targetHeight, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap( bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()),
new Rect(0, 0, targetWidth, targetHeight), paint);
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
Bitmap resizedBitmap = Bitmap.createBitmap(targetBitmap, 0, 0, 30, 30, matrix, true);
Bitmap bitmap_circle=mergeBitmaps(resizedBitmap);
BitmapDrawable bd = new BitmapDrawable(bitmap_circle);
return bd;
}
The above function will create the final drawable for the marker.Also the mergeBitmaps() function merge the both the resource drawable and the bit map together..
public Bitmap mergeBitmaps(Bitmap manBitmap){
try{
Bitmap markerBitmap = BitmapFactory.decodeResource( this.getResources(), R.drawable.circle_bg);
Bitmap bmOverlay = Bitmap.createBitmap(markerBitmap.getWidth(), markerBitmap.getHeight(), markerBitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
canvas.drawBitmap(markerBitmap, matrix, null);
canvas.drawBitmap(manBitmap, 5, 5, null);
return bmOverlay;
}
catch(Exception ex){
ex.printStackTrace();
return null;
}
}
But the problem is, this bitmap is not best fit inside the background Drawable in order to get a feeling that both together will give a single image.
Can anyone help me ?
change
canvas.drawBitmap(manBitmap, 5, 5, null);
to smtg like
canvas.drawBitmap(manBitmap, (markerBitmap.getWidth()-manbitmap.getWidth())/2,
(markerBitmap.getHeight()-manbitmap.getHeight())/2, null);

Categories

Resources