用手指随便拖。这里采用了线程去绘制,其实也可以在onTouch中绘制。
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.graphics.Rect; import android.graphics.Region; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnTouchListener; import com.ql.app.R; public class AppView extends SurfaceView implements SurfaceHolder.Callback,Runnable,OnTouchListener{ private static final String tag="AppView"; private Context context; private SurfaceHolder holder; private Bitmap icon; private Paint paint; private boolean running=true; public AppView(Context context) { super(context); // TODO Auto-generated constructor stub this.context=context; holder = this.getHolder();//获取holder holder.addCallback(this); this.setOnTouchListener(this); } private void init(){ } private void close(){ } private void logic(){ } private void draw(){ Canvas canvas = holder.lockCanvas();//获取画布 canvas.drawColor(Color.BLACK); canvas.drawBitmap(icon, rect.left,rect.top,null); holder.unlockCanvasAndPost(canvas);// 解锁画布,提交画好的图像 } @Override public void surfaceCreated(SurfaceHolder holder) { // TODO Auto-generated method stub Log.i(tag, "=================surfaceCreated======================"); icon =getImage(context,R.drawable.icon); rect=new Rect(0,0,icon.getWidth(),icon.getHeight());//图片的rect paint=new Paint(); running=true; new Thread(this).start(); } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub Log.i(tag, "=================surfaceDestroyed======================"); running=false; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // TODO Auto-generated method stub Log.i(tag, "=================surfaceChanged======================"); } @Override public void run() { // TODO Auto-generated method stub int SLEEP_TIME=100; while (running) { long start=System.currentTimeMillis(); logic(); draw(); long end=System.currentTimeMillis(); if(end-start<SLEEP_TIME){ try { Thread.sleep(SLEEP_TIME-(end-start)); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } // Region region=new Region(); private Point point=new Point();//点击点 private Rect rect;//图片的rect private boolean canDrag=false;//判断是否点击在图片上,否则拖动无效 private int offsetX=0,offsetY=0;//点击点离图片左上角的距离 @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case MotionEvent.ACTION_DOWN: point.x=(int)event.getX(); point.y=(int)event.getY(); if(rect.contains(point.x, point.y)){ canDrag=true; offsetX=point.x-rect.left; offsetY=point.y-rect.top; } break; case MotionEvent.ACTION_MOVE: if(canDrag){ rect.left=(int)event.getX()-offsetX; rect.top=(int)event.getY()-offsetY; rect.right=rect.left+icon.getWidth(); rect.bottom=rect.top+icon.getHeight(); if (rect.left < 0) { rect.left = 0; rect.right = rect.left+icon.getWidth(); } if (rect.right > getMeasuredWidth()) { rect.right = getMeasuredWidth(); rect.left = rect.right-icon.getWidth(); } if (rect.top < 0) { rect.top = 0; rect.bottom = rect.top+icon.getHeight(); } if (rect.bottom > getMeasuredHeight()) { rect.bottom = getMeasuredHeight(); rect.top = rect.bottom-icon.getHeight(); } } break; case MotionEvent.ACTION_UP: canDrag=false; break; default: break; } return true; } //**************************************************************************** public static final Bitmap getImage(Context context, int imageId) { return BitmapFactory.decodeResource(context.getResources(),imageId); } }
//在Activity中调用:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AppView view=new AppView(this);
setContentView(view);
}
这个主要是根据通知自定义布局开显示的
只需要在示例1中把
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,R.string.app_name,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
n.setLatestEventInfo(MainActivity.this, "有急事", "请您马上到办公司开会", pi);
mn.notify(R.string.app_name, n);
替换成
PendingIntent pi = PendingIntent.getActivity(MainActivity.this,R.string.app_name,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews rv = new RemoteViews(MainActivity.this.getPackageName(),R.layout.notification_view);
rv.setImageViewResource(R.id.image, R.drawable.icon);
rv.setTextViewText(R.id.text, "嗨,美女,你好啊");
n.contentView =rv;
n.contentIntent=pi;
mn.notify(R.string.app_name, n);
即可实现两者的效果,
主要区别,示例1是系统自动显示的,示例2是可以自己布局显示
然后配置notification_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView android:id="@+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView android:id="@+id/image" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Android NDK r6b
Windows
http://dl.google.com/android/ndk/android-ndk-r6b-windows.zip
Mac OS X(intel)
http://dl.google.com/android/ndk/android-ndk-r6b-darwin-x86.tar.bz2
Linux
http://dl.google.com/android/ndk/android-ndk-r6b-linux-x86.tar.bz2
Android SDK 3.2
Windows
http://dl.google.com/android/android-sdk_r12-windows.zip
http://dl.google.com/android/installer_r12-windows.exe
Mac OS X (intel)
http://dl.google.com/android/android-sdk_r12-mac_x86.zip
Linux(i386)
http://dl.google.com/android/android-sdk_r12-linux_x86.tgz
Android NDK r6
Windows
http://dl.google.com/android/ndk/android-ndk-r6-windows.zip
Mac OS X(intel)
http://dl.google.com/android/ndk/android-ndk-r6-darwin-x86.tar.bz2
Linux 32/64-bit (x86)
http://dl.google.com/android/ndk/android-ndk-r6-linux-x86.tar.bz2
Android SDK 3.1
Windows
http://dl.google.com/android/android-sdk_r11-windows.zip
http://dl.google.com/android/installer_r11-windows.exe
Mac OS X(intel)
http://dl.google.com/android/android-sdk_r11-mac_x86.zip
Linux(i386)
http://dl.google.com/android/android-sdk_r11-linux_x86.tgz
Android NDK r5b
Windows
http://dl.google.com/android/ndk/android-ndk-r5b-windows.zip
Mac OS X(intel)
http://dl.google.com/android/ndk/android-ndk-r5b-darwin-x86.tar.bz2
Linux
http://dl.google.com/android/ndk/android-ndk-r5b-linux-x86.tar.bz2
Android SDK 3.0
Windows
http://dl.google.com/android/installer_r10-windows.exe
http://dl.google.com/android/android-sdk_r10-windows.zip
Mac OS X(intel)
http://dl.google.com/android/android-sdk_r10-mac_x86.zip
Linux(i386)
http://dl.google.com/android/android-sdk_r10-linux_x86.tgz
Android SDK 2.3
Windows
http://dl.google.com/android/android-sdk_r08-windows.zip
http://dl.google.com/android/installer_r08-windows.exe
Linux (i386)
http://dl.google.com/android/android-sdk_r08-linux_86.tgz
MAC OS X (intel)
http://dl.google.com/android/android-sdk_r08-mac_86.zip
Android NDK r5
Windows
http://dl.google.com/android/ndk/android-ndk-r5-windows.zip
Mac OS X (intel) r5
http://dl.google.com/android/ndk/android-ndk-r5-darwin-x86.tar.bz2
Linux 32/64-bit (x86) r5
http://dl.google.com/android/ndk/android-ndk-r5-linux-x86.tar.bz2