import java.io.File;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.hardware.SensorListener;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.view.View;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
class Pint {
private Drawable bgFull, bgEmpty, bgFront;
private Bitmap foam;
private static final float WIDTH = 320;
private static final float HEIGHT = 480;
private static final float TOTAL = WIDTH * HEIGHT - 50 * 320;
private float currentVolume = 0;
float leftY = 480, rightY = 480;
float foamHeight;
public Pint(Drawable bgFull, Drawable bgEmpty, Drawable bgFront, Bitmap foam) {
this.bgEmpty = bgEmpty;
this.bgEmpty.setBounds(0, 0, bgEmpty.getIntrinsicWidth(),
bgEmpty.getIntrinsicHeight());
this.bgFull = bgFull;
this.bgFull.setBounds(0, 0, bgFull.getIntrinsicWidth(),
bgFull.getIntrinsicHeight());
this.bgFront = bgFront;
this.bgFront.setBounds(0, 0, bgFront.getIntrinsicWidth(),
bgFront.getIntrinsicHeight());
this.foam = foam;
foamHeight = foam.getHeight() / 2;
}
private float angle = 0f;
private boolean full = false;
public void setAngle(float angle) {
this.angle = angle;
}
public float getCurrentVolume() {
return currentVolume;
}
public void setCurrentVolume(float currentVolume) {
this.currentVolume = currentVolume;
}
public void draw(Canvas canvas) {
bgEmpty.draw(canvas);
canvas.save();
float emptyHeight = 0;
if (!full) {
currentVolume += (10 * 320);
if (currentVolume > TOTAL) {
full = true;
}
}
emptyHeight = (float) (Math.tan(Math.toRadians(angle)) * WIDTH);
float adjustedHeight = currentVolume / WIDTH; // normal height
adjustedHeight = adjustedHeight <= 0 ? 2f : adjustedHeight;
leftY = Math.max(-1000, HEIGHT - (adjustedHeight + emptyHeight));
rightY = Math.max(-1000, HEIGHT - (adjustedHeight - emptyHeight));
if (currentVolume > WIDTH) {
Path p = new Path();
p.moveTo(0, HEIGHT); // lower left
p.lineTo(WIDTH, HEIGHT);
p.lineTo(WIDTH, rightY);
p.lineTo(0, leftY);
canvas.clipPath(p);
bgFull.draw(canvas);
canvas.restore();
canvas.drawBitmapMesh(foam, 1, 1, new float[] { 0,
leftY - foamHeight, 320, rightY - foamHeight, 0,
leftY + foamHeight, 320, rightY + foamHeight, }, 0, null,
0, null);
}
if (leftY < 5 || rightY < 5) {
currentVolume *= 0.98;// -= (2000f);
currentVolume -= 200f;
}
bgFront.draw(canvas);
}
}
public class PintView extends View implements SensorListener {
private Pint pint;
private MediaPlayer mp;
private Context ctx;
public PintView(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
String skinName = settings.getString("skin", "Default");
loadSkin(skinName);
playRefillSound();
}
public PintView(Context context) {
super(context);
}
private boolean more = true;
private void playRefillSound() {
mp = MediaPlayer.create(ctx, R.raw.refill);//refill.mp3
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
public void loadSkin(String name) {
File skinPath = new File(new File("/sdcard/ADRINK"), name);
float volume = pint == null ? 0 : pint.getCurrentVolume();
pint = null;
Drawable full = Drawable.createFromPath(skinPath.getAbsolutePath()
+ "/" + "full.png");
Drawable empty = Drawable.createFromPath(skinPath.getAbsolutePath()
+ "/" + "empty.png");
Drawable front = Drawable.createFromPath(skinPath.getAbsolutePath()
+ "/" + "front.png");
Bitmap foam = BitmapFactory.decodeFile(skinPath.getAbsolutePath()
+ "/" + "foam.png");
pint = new Pint(full, empty, front ,foam );
pint.setCurrentVolume(volume);
}
public Pint getPint() {
return pint;
}
@Override
protected void onDraw(Canvas canvas) {
pint.draw(canvas);
if (more) {
invalidate();
}
}
public void setMore(boolean more) {
this.more = more;
}
@Override
public void onAccuracyChanged(int sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(int sensor, float[] values) {
float angle = values[2];
pint.setAngle(0 - angle);
}
}