这里记录一些自己平时开发用到的知识点,仅供备忘和快速查找使用。描述中省略细节。
★ Android使用Application总结
http://blog.youkuaiyun.com/renguichao/article/details/7667245#comments
★ soundpool 播放音乐
Android中的soundpool小结
http://jackyrong.iteye.com/blog/1008476
★back键 确认对话框
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK )
{
ExitDialog();
}
return super.onKeyDown(keyCode, event);
}
private void ExitDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.exit_game);
builder.setPositiveButton(R.string.win_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击确定后的逻辑
}
});
builder.setNegativeButton(R.string.win_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//这里添加点击确定后的逻辑
}
});
builder.create().show();
}
★延迟 简单的实现方式
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
★Dialog的使用
Android软件开发之盘点所有Dialog对话框大合集(一)
参见:http://xys289187120.blog.51cto.com/3361352/657562/
★Surface的使用
Android之SurfaceView学习(一)
http://www.cnblogs.com/xuling/archive/2011/06/06/android.html
★Canvas 的使用
参见: Android Canvas绘图详解(图文)
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1212/703.html
动态显示一个静态图
canvas.clipRect(fly_x, fly_y, fly_x + fly.getWidth(), fly_y+ fly_height);
canvas.drawBitmap(fly, fly_x, fly_y - currentFrame * fly_height,paint);
currentFrame++;
if (currentFrame >= 3) {
currentFrame = 0;
}
canvas.restore();
★ 让自己的view动起来
public class MainView implements Runnable
private Thread thread;
thread = new Thread(this);
thread.start();
// @Override
public void run() {
// TODO Auto-generated method stub
while (threadFlag) {
long begintime = System.currentTimeMillis();
initObject();
drawSelf();
long endtime = System.currentTimeMillis() ;
if(100-(endtime-begintime)>0){
try {
Thread.sleep(100-(endtime-begintime));
} catch (InterruptedException e) {
throw new WOTPException(e);
}
}
}
}
public void drawSelf() {
//do something after interval
}
★ 资源图片 转成 bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.test);
★ 强制横屏 , 竖屏
AndroidManifest.xml中找到你所指定的activity中加上android:screenOrientation属性,"landscape":横屏显示(宽比高要长) ,"portrait":竖屏显示(高比宽要长)
★ 不显示标题栏
AndroidManifest.xml中找到你所指定的activity中加上android:screenOrientation属性,android:theme="@android:style/Theme.NoTitleBar"
★ 截取部分屏幕 生成图片
View view = getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
Bitmap newBitmap = Bitmap.createBitmap(bitmap);
view.setDrawingCacheEnabled(false);
LinearLayout ll = (LinearLayout)findViewById(R.id.ll_current);
int theight = ll.getHeight()/2+15;
mOpponentPicture.setImageBitmap(Bitmap.createBitmap(newBitmap, 0, theight, width, width+iwidth/2-theight));
★ 图片压缩的方法
BitmapFactory.Options options=new Options();
options.inDither=false; /*不进行图片抖动处理*/
options.inPreferredConfig=null; /*设置让解码器以最佳方式解码*/
options.inSampleSize=4; /*图片长宽方向缩小倍数*/
Bitmap img=BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
★ 图片 内存泄露
使用Bitmap后内存很容易出现泄露的问题,所以需要调用recycle()方法来对内存进行回收。
if(!bitmap.isRecycled(){
bitmap.recycle()
}
★ 图片 压缩
public static byte[] imgBitmapByteJpg(Bitmap bitmap){
if(bitmap ==null)
return null ;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);
return baos.toByteArray();
}
★ 全局alertDialog
context那个参数传 getapplication
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
★ Activity UI 的setcontentview
一共有三种形式:
voidsetContentView(int layoutResID) //Set the activity content from a layout resource.
void setContentView(View view) //Set the activity content to an explicit view.
void setContentView(View view, ViewGroup.LayoutParams params) //Set the activity content to an explicit view.
eg.
mainView = new MainView(this , wotpContext); //this 传入到view中,提供view和activity之间的沟通
setContentView(mainView);