Compressing converting a bitmap to PNG format Android example
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
int quality) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
src.compress(format, quality, os);
byte[] array = os.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawColor(Color.YELLOW);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
Bitmap bPNGcompress = codec(bmp, Bitmap.CompressFormat.PNG, 0);
int h = bmp.getHeight();
canvas.drawBitmap(bmp, 10,10, paint);
canvas.drawBitmap(bPNGcompress, 10,10 + h + 10, paint);
}
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
If you using Context as parameter of function try this solution:
public class MyActivity extends Activity {
// bla bla bla .......
//error
myFc( getapplicationcontext());
// OK
myFc(MyActivity.this);
////www.apache.org/licenses/LICENSE-2.0
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private ColorMatrix mCM = new ColorMatrix();
private Bitmap mBitmap;
private float mSaturation;
private float mAngle;
//..........
mBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.my_image);
//..........
private static void setContrastScaleOnly(ColorMatrix cm, float contrast) {
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
cm.set(new float[] {
scale, 0, 0, 0, 0,
0, scale, 0, 0, 0,
0, 0, scale, 0, 0,
0, 0, 0, 1, 0 });
}
private static void setContrast(ColorMatrix cm, float contrast) {
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
cm.set(new float[] {
scale, 0, 0, 0, translate,
0, scale, 0, 0, translate,
0, 0, scale, 0, translate,
0, 0, 0, 1, 0 });
}
private static void setContrastTranslateOnly(ColorMatrix cm, float contrast) {
float scale = contrast + 1.f;
float translate = (-.5f * scale + .5f) * 255.f;
cm.set(new float[] {
1, 0, 0, 0, translate,
0, 1, 0, 0, translate,
0, 0, 1, 0, translate,
0, 0, 0, 1, 0 });
}
@Override protected void onDraw(Canvas canvas) {
Paint paint = mPaint;
float x = 20;
float y = 20;
canvas.drawColor(Color.WHITE);
paint.setColorFilter(null);
canvas.drawBitmap(mBitmap, x, y, paint);
ColorMatrix cm = new ColorMatrix();
mAngle += 2;
if (mAngle > 180) {
mAngle = 0;
}
//convert our animated angle [-180...180] to a contrast value of [-1..1]
float contrast = mAngle / 180.f;
setContrast(cm, contrast);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint);
setContrastScaleOnly(cm, contrast);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(mBitmap, x, y + mBitmap.getHeight() + 10, paint);
setContrastTranslateOnly(cm, contrast);
paint.setColorFilter(new ColorMatrixColorFilter(cm));
canvas.drawBitmap(mBitmap, x, y + 2*(mBitmap.getHeight() + 10),
paint);
invalidate();
}
}
Delete - unistal app from testing device - emulator and try again RUN - DEBUGG your app.
Android development
long is 64 bit signed type and used when int is not large enough to hold the value.
long je celé číslo 64 bitů -9223372036854775808 +9223372036854775807 a používá se tam, kde typ int není schopen pojmout takovou hodnotu čísla.
long is 64 bit signed type and used when int is not large enough to hold the value.
long je celé číslo 64 bitů -9223372036854775808 +9223372036854775807 a používá se tam, kde typ int není schopen pojmout takovou hodnotu čísla.
// declaration and assignment of value type long
long n = 22337203685477580L;
// print formated value
System.out.printf("The value of x is %d%n", n); // 22337203685477580
System.out.format("%+,8d%n%n", n); // +22 337 203 685 477 580
// declaring more variables in single statement
long lo1 = 12L, lo2 = 56, lo3 = 1455555555589L;
// long range of value
System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println(Long.MIN_VALUE); // -9223372036854775808
// check if a string is a valid number in Java example
// convert string to long Java example
String sLong = "1288888888888888";
long longParse = Long.parseLong(sLong);
// convert strings to numbers
long longFromString = (Long.valueOf(sLong)).longValue();
// long to string in Java example code
Long longObj = new Long(229999999999L);
String str = longObj.toString();
// else
Long longS = 888888888888L;
String strLong = longS.toString();
// compare two long variables
Long longComp1 = 5555L;
if (longComp1.equals(55555555L))
System.out.print("true");
// compares the two specified long values in Java example
int i = longS.compareTo(444444L); // -1 first < second
// 0 first == second
// 1 first > second
System.out.print(i);
Cycle for in Java example
public class MainClass {
public static void main(String[] arg) {
String[] arrayOfString = { "Hello", "people", "hello", "world!" };
for (int i = 0; // start position , first cycle i == 0
i < arrayOfString.length; // if i < 4 (length of array) do loop
i++ // incrementation after cycle
){
System.out.println(arrayOfString[i]);
// i++ incrementation
}
}
}
Editace: 2013-12-09 13:09:31
Počet článků v kategorii: 396
Url:compressing-converting-a-bitmap-to-png-format-android-example