Created
July 13, 2011 22:58
-
-
Save evandrix/1081530 to your computer and use it in GitHub Desktop.
Working with Images in Android
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.exercise.AndroidPaint; | |
import android.app.Activity; | |
import android.content.Context; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.graphics.Canvas; | |
import android.os.Bundle; | |
import android.view.View; | |
public class AndroidPaint extends Activity { | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
//setContentView(R.layout.main); | |
setContentView(new myView(this)); | |
} | |
private class myView extends View{ | |
public myView(Context context) { | |
super(context); | |
// TODO Auto-generated constructor stub | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
// TODO Auto-generated method stub | |
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.googlelogo320x480); | |
canvas.drawBitmap(myBitmap, 0, 0, null); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public boolean onKeyDown(int keyCode, KeyEvent event) { | |
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) { | |
// Perform the tinting operation | |
TintThePicture(20); | |
// Display a short message on screen | |
nm.notifyWithText(56789, "Picture was tinted", | |
NotificationManager.LENGTH_SHORT, null); | |
// Save the result | |
SaveThePicture(); | |
return (true); | |
} | |
return super.onKeyDown(keyCode, event); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Banner extends View { | |
private final Drawable logo; | |
public Banner(Context context) { | |
super(context); | |
logo = context.getResources().getDrawable(R.drawable.banner); | |
setBackgroundDrawable(logo); | |
} | |
public Banner(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
logo = context.getResources().getDrawable(R.drawable.banner); | |
setBackgroundDrawable(logo); | |
} | |
public Banner(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
logo = context.getResources().getDrawable(R.drawable.banner); | |
setBackgroundDrawable(logo); | |
} | |
@Override protected void onMeasure(int widthMeasureSpec, | |
int heightMeasureSpec) { | |
int width = MeasureSpec.getSize(widthMeasureSpec); | |
int height = width * logo.getIntrinsicHeight() / logo.getIntrinsicWidth(); | |
setMeasuredDimension(width, height); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void TintThePicture(int deg) { | |
int[] pix = new int[picw * pich]; | |
mBitmap.getPixels(pix, 0, picw, 0, 0, picw, pich); | |
int RY, GY, BY, RYY, GYY, BYY, R, G, B, Y; | |
double angle = (3.14159d * (double)deg) / 180.0d; | |
int S = (int)(256.0d * Math.sin(angle)); | |
int C = (int)(256.0d * Math.cos(angle)); | |
for (int y = 0; y < pich; y++) | |
for (int x = 0; x < picw; x++) | |
{ | |
int index = y * picw + x; | |
int r = (pix[index] >> 16) & 0xff; | |
int g = (pix[index] >> 8) & 0xff; | |
int b = pix[index] & 0xff; | |
RY = ( 70 * r - 59 * g - 11 * b) / 100; | |
GY = (-30 * r + 41 * g - 11 * b) / 100; | |
BY = (-30 * r - 59 * g + 89 * b) / 100; | |
Y = ( 30 * r + 59 * g + 11 * b) / 100; | |
RYY = (S * BY + C * RY) / 256; | |
BYY = (C * BY - S * RY) / 256; | |
GYY = (-51 * RYY - 19 * BYY) / 100; | |
R = Y + RYY; | |
R = (R < 0) ? 0 : ((R > 255) ? 255 : R); | |
G = Y + GYY; | |
G = (G < 0) ? 0 : ((G > 255) ? 255 : G); | |
B = Y + BYY; | |
B = (B < 0) ? 0 : ((B > 255) ? 255 : B); | |
pix[index] = 0xff000000 | (R << 16) | (G << 8) | B; | |
} | |
Bitmap bm = Bitmap.createBitmap(picw, pich, false); | |
bm.setPixels(pix, 0, picw, 0, 0, picw, pich); | |
// Put the updated bitmap into the main view | |
mIV.setImageBitmap(bm); | |
mIV.invalidate(); | |
mBitmap = bm; | |
pix = null; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
READ | |
---- | |
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), | |
R.drawable.pic1); | |
int pic_width = mBitmap.width(); | |
int pic_height = mBitmap.height(); | |
***** | |
WRITE | |
----- | |
try { | |
FileOutputStream fos = super.openFileOutput("output.jpg", | |
MODE_WORLD_READABLE); | |
mBitmap.compress(CompressFormat.JPEG, 75, fos); | |
fos.flush(); | |
fos.close(); | |
} catch (Exception e) { | |
Log.e("MyLog", e.toString()); | |
} | |
***** | |
main.xml | |
-------- | |
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/ | |
apk/res/android" | |
android:orientation="vertical" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
> | |
<ImageView id="@+id/picview" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
/> | |
</LinearLayout> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment