我们可以继承ImageView等view通过onDraw来将你想要画的东西画到canvas上,然后通过Invalidate来是canvas重画。
下面是平滑移动图片的效果:
//继承imageView
package com.android.ActivityAlbum;
import java.util.ArrayList;
import com.android.ActivityAlbum.AlbumWidget.UpdateService;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SliderImageView extends ImageView {
private Bitmap lbitmap;
private Bitmap rbitmap;
private Drawable ldrawable;
private Drawable rdrawable;
private static ArrayList<String> photos = new ArrayList<String>();
private static int photo_count = 0;
private static int time=1;
private Resources res;
public static int length = 5;
int mlength=0;
public SliderImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
photos = AlbumWidget.photos;
res=getResources();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// drawable=Slider.drawable;
if (photo_count != photos.size()) {
if(time==1){
ldrawable = UpdateService.getDrawable(res, photo_count);
lbitmap = Utilities.createIconThumbnail(ldrawable, 230, 350);
if (photo_count != photos.size() - 1) {
rdrawable = UpdateService.getDrawable(res, photo_count+1);
rbitmap = Utilities.createIconThumbnail(rdrawable, 230, 350);
} else {
rdrawable = UpdateService.getDrawable(res, 0);
rbitmap = Utilities.createIconThumbnail(rdrawable, 230, 350);
}
}
canvas.drawBitmap(lbitmap, -mlength, 0, null);
canvas.drawBitmap(rbitmap, 230-mlength, 0, null);
if(time==47){
photo_count++;
time=1;
mlength=0;
}
else{
time++;
mlength=mlength+length;
}
} else {
photo_count = 0;
}
}
}
在Activity里通过一个thread和handler来使调用invalidate
class photothread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
Message msg = new Message();
msg.what = UPDATEPHOTO;
photohandler.sendMessage(msg);
}
}
Handler photohandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATEPHOTO:
mimgvp.invalidate();
// System.out.println("update photos");
}
}
};
下面是平滑移动图片的效果:
//继承imageView
package com.android.ActivityAlbum;
import java.util.ArrayList;
import com.android.ActivityAlbum.AlbumWidget.UpdateService;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SliderImageView extends ImageView {
private Bitmap lbitmap;
private Bitmap rbitmap;
private Drawable ldrawable;
private Drawable rdrawable;
private static ArrayList<String> photos = new ArrayList<String>();
private static int photo_count = 0;
private static int time=1;
private Resources res;
public static int length = 5;
int mlength=0;
public SliderImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
photos = AlbumWidget.photos;
res=getResources();
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
// drawable=Slider.drawable;
if (photo_count != photos.size()) {
if(time==1){
ldrawable = UpdateService.getDrawable(res, photo_count);
lbitmap = Utilities.createIconThumbnail(ldrawable, 230, 350);
if (photo_count != photos.size() - 1) {
rdrawable = UpdateService.getDrawable(res, photo_count+1);
rbitmap = Utilities.createIconThumbnail(rdrawable, 230, 350);
} else {
rdrawable = UpdateService.getDrawable(res, 0);
rbitmap = Utilities.createIconThumbnail(rdrawable, 230, 350);
}
}
canvas.drawBitmap(lbitmap, -mlength, 0, null);
canvas.drawBitmap(rbitmap, 230-mlength, 0, null);
if(time==47){
photo_count++;
time=1;
mlength=0;
}
else{
time++;
mlength=mlength+length;
}
} else {
photo_count = 0;
}
}
}
在Activity里通过一个thread和handler来使调用invalidate
class photothread implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
Message msg = new Message();
msg.what = UPDATEPHOTO;
photohandler.sendMessage(msg);
}
}
Handler photohandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case UPDATEPHOTO:
mimgvp.invalidate();
// System.out.println("update photos");
}
}
};