Photo Widget своими руками
public class WidgetView extends View {
ContentObserver observer;
GestureDetector gd;
Scroller scroller;
NinePatchDrawable frame;
Bitmap loading;
Bitmap broken;
Item[] items = null;
float touchLastY = 0;
float touchDownY = 0;
boolean touchTap = false;
boolean touchScroll = false;
long loaderId = -1;
int getScrollY = 0;
int width = 160;
int height = 200;
public WidgetView (Context context) {
super (context);
gd = new GestureDetector (context, new GestureListener ());
scroller = new Scroller (context);
observer = new ContentObserver (new Handler ()){
@Override
public void onChange (boolean selfChange){
super.onChange (selfChange);
loadThumbnails ();
}
};
frame = (NinePatchDrawable)getResources ().getDrawable (R.drawable.frame);
loading = getBmp (R.drawable.loading);
broken = getBmp (R.drawable.broken);
}
@Override
public void onAttachedToWindow (){
super.onAttachedToWindow ();
loadThumbnails ();
getContext ().getContentResolver ().registerContentObserver (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, observer);
getContext ().getContentResolver ().registerContentObserver (MediaStore.Video.Media.EXTERNAL_CONTENT_URI, true, observer);
}
@Override
public void onDetachedFromWindow (){
super.onDetachedFromWindow ();
getContext ().getContentResolver ().unregisterContentObserver (observer);
}
@Override
protected void onDraw (Canvas canvas){
super.onDraw (canvas);
Paint p = new Paint ();
p.setDither (true);
p.setFilterBitmap (true);
int pos = getSelected ();
float percent = getSelectedPercent ();
if (items!= null){
if (items.length == 0){
Bitmap camera = getBmp (R.drawable.camera);
Transformation transform = new Transformation (0, percent, 160, 140);
p.setAlpha (transform.alpha);
canvas.drawBitmap (camera, transform.matrix3d, p);
return;
}
if (pos >= 1){
if (items[pos-1] != null){
if (items[pos-1].thumbnail!= null){
Bitmap thumbnail = items[pos-1].thumbnail;
Transformation transform = new Transformation (-1, percent, thumbnail.getWidth (), thumbnail.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (thumbnail, transform.matrix3d, p);
} else {
Transformation transform = new Transformation (-1, percent, broken.getWidth (), broken.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (broken, transform.matrix3d, p);
}
} else {
Transformation transform = new Transformation (-1, percent, loading.getWidth (), loading.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (loading, transform.matrix3d, p);
}
}
if (items.length-1 >= pos+1){
if (items[pos+1] != null){
if (items[pos+1].thumbnail!= null){
Bitmap thumbnail = items[pos+1].thumbnail;
Transformation transform = new Transformation (1, percent, thumbnail.getWidth (), thumbnail.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (thumbnail, transform.matrix3d, p);
} else {
Transformation transform = new Transformation (1, percent, broken.getWidth (), broken.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (broken, transform.matrix3d, p);
}
} else {
Transformation transform = new Transformation (1, percent, loading.getWidth (), loading.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (loading, transform.matrix3d, p);
}
}
if (items.length-1 >= pos){
if (items[pos] != null){
if (items[pos].thumbnail!= null){
Bitmap thumbnail = items[pos].thumbnail;
Transformation transform = new Transformation (0, percent, thumbnail.getWidth (), thumbnail.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (thumbnail, transform.matrix3d, p);
} else {
Transformation transform = new Transformation (0, percent, broken.getWidth (), broken.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (broken, transform.matrix3d, p);
}
} else {
Transformation transform = new Transformation (0, percent, loading.getWidth (), loading.getHeight ());
p.setAlpha (transform.alpha);
canvas.drawBitmap (loading, transform.matrix3d, p);
}
}
} else {
Transformation transform = new Transformation (0, 0, loading.getWidth (), loading.getHeight ());
canvas.drawBitmap (loading, transform.matrix3d, null);
}
}
@Override
public boolean onTouchEvent (MotionEvent me) {
if (items == null) return true;
gd.onTouchEvent (me);
float touchY = me.getY ();
if (me.getAction () == MotionEvent.ACTION_DOWN){
if (! scroller.isFinished ()){
scroller.abortAnimation ();
} else {
touchTap = true;
touchScroll = false;
}
touchDownY = touchLastY = touchY;
} else if (me.getAction () == MotionEvent.ACTION_MOVE){
if (Math.abs (touchY — touchDownY) > ViewConfiguration.getTouchSlop () || touchScroll){
getParent ().requestDisallowInterceptTouchEvent (true);
touchTap = false;
touchScroll = true;
getScrollY += -(int)(touchY — touchLastY);
computeOverscroll ();
}
touchLastY = touchY;
} else if ((me.getAction () == MotionEvent.ACTION_UP || me.getAction () == MotionEvent.ACTION_CANCEL) && scroller.isFinished ()){
if (touchTap && me.getAction () == MotionEvent.ACTION_UP){
tap ();
}
setSelected (getSelected (), true);
getParent ().requestDisallowInterceptTouchEvent (false);
}
return true;
}
@Override
public void computeScroll (){
if (scroller.computeScrollOffset ()){
int oldY = getScrollY;
getScrollY = scroller.getCurrY ();
if (oldY!= getScrollY){
onScrollChanged (0, getScrollY, 0, oldY);
}
if (scroller.getFinalY () == getScrollY){
scroller.abortAnimation ();
setSelected (getSelected (), true);
}
postInvalidate ();
}
}
@Override
protected void onMeasure (int wms, int hms){
width = MeasureSpec.getSize (wms);
height = MeasureSpec.getSize (hms);
super.onMeasure (wms, hms);
}
@Override
protected void onSizeChanged (int w, int h, int oldw, int oldh){
width = w;
height = h;
super.onSizeChanged (w, h, oldw, oldh);
}
void loadThumbnails (){
items = null;
getScrollY = 0;
invalidate ();
Thread loader = new Thread (new Runnable () {
public void run () {
long currentId = Thread.currentThread ().getId ();
try{
final ArrayList