—————————————————索引—————————————————
1.图片的压缩
2.showNoNetworkView 无网络提示,纯代码控制
3.国际化判断语言
4.Handler的几种用法
5.程序中获取内存信息
6.媒体文件通知媒体数据库更新
7.禁用硬件加
8.在同一layout中切换Fragment(不重新加载fragment)
9.点击振动
10.android 等比例缩放图片
11.Android系统自带样式Android:theme
12.android状态栏透明化【全屏】
13.Android 按下home键,程序在后台运行,在launcher里面再点击icon又重新启动了,解决方法。
14.Android 更新文件到媒体库。
1.图片的压缩;
rootView = (RelativeLayout) findViewById(R.id.root);
txNote = (TextView) findViewById(R.id.tx_note);
// 优化background防内存溢出
BitmapFactory.Options opts = new BitmapFactory.Options();
// 级别越高压缩率越高
opts.inSampleSize = 2;
Bitmap b = BitmapFactory.decodeResource(getResources(),R.drawable.splash_bg, opts);
rootView.setBackground(new BitmapDrawable(b));
2.showNoNetworkView 无网络提示,纯代码控制;
/**
* showNoNetworkView 无网络
* 2015.07.22
* zx
*/
private void showNoNetworkView() {
LinearLayout subLayout = null;
subLayout = new LinearLayout(this);
subLayout.setOrientation(LinearLayout.VERTICAL);
// no wifi
LinearLayout.LayoutParams imgVPam = new LinearLayout.LayoutParams(180, 180);
imgVPam.gravity = Gravity.CENTER_HORIZONTAL;
final ImageView imgV = new ImageView(this);
imgV.setImageResource(R.drawable.hint_no_wifi);
subLayout.addView(imgV, imgVPam);
// On Click
LinearLayout.LayoutParams btPam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
btPam.gravity = Gravity.CENTER_HORIZONTAL;
btPam.setMargins(0, 10, 0, 0);
Button btn = new Button(this);
btn.setText(R.string.btn_click_load);
//TextPaint tp = btn.getPaint();
//tp.setFakeBoldText(true);
btn.setTextSize(16);
btn.setTextColor(Color.parseColor("#969696"));
btn.setGravity(Gravity.CENTER);
btn.setBackgroundResource(R.drawable.btn_click_load_selector);
btn.setPadding(55, 10, 55, 10);
subLayout.addView(btn, btPam);
// Error Msg
LinearLayout.LayoutParams textPam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textPam.gravity = Gravity.CENTER_HORIZONTAL;
textPam.setMargins(0, 20, 0, 0);
TextView text = new TextView(this);
text.setText(R.string.warning_network_fail);
text.setTextColor(Color.parseColor("#C1C1C1"));
text.setGravity(Gravity.CENTER);
text.setTextSize(15);
subLayout.addView(text, textPam);
// add view
RelativeLayout.LayoutParams subPam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
subPam.setMargins(0, 0, 0, 80);
subPam.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
subPam.addRule(RelativeLayout.CENTER_HORIZONTAL);
rootLayout.addView(subLayout, subPam);
/*btn.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View arg0) {
if (NetworkState.networkIsOpen(SplashActivity.this)) {
if(txNote.getVisibility() == View.VISIBLE){
txNote.setVisibility(View.GONE);
}
subLayout.setVisibility(View.INVISIBLE);
doReq();
} else {
Toasts.showToast(getString(R.string.network_unopen));
}
}
});*/
}
3.国际化判断语言;
String lan = Locale.getDefault().getLanguage();
if ("zh".equalsIgnoreCase(lan) || "ch".equalsIgnoreCase(lan)|| "tw".equalsIgnoreCase(lan)) {
btnPlayer.setImageResource(R.drawable.play_draw);
} else {
btnPlayer.setImageResource(R.drawable.play_en_draw);
}
4.Handler Looper 的几种用法;
/****************************************
* 1.new myLooper
* 用HandlerThread写一个自己的消息队列Looper
*/
HandlerThread myHandlerThread = new HandlerThread("myHandlerThread");
Looper myLooper = myHandlerThread.getLooper();
Handler myHandler = new Handler(myLooper){
@Override
public void handleMessage(Message msg) {
sendMessage(msg);
};
};
/****************************************
* 2.new myLooper
* 用 new Thread写一个自己的消息队列Looper
*/
Thread myThread = new Thread() {
@Override
public void run() {
Looper.prepare();
Handler myHandler = new Handler(Looper.myLooper()){
@Override
public void handleMessage(Message msg) {
};
};
Looper.loop();
}
};
myThread.start();
/****************************************
* 3.Handler 的Callback
*/
Handler myHandler0 = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message arg0) {
return false;
}
});
5.程序中获取内存信息;
private void displayBriefMemory()
{
final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
ActivityManager.MemoryInfo info = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(info);
Log.i(tag,"系统剩余内存:"+(info.availMem >> 10)+"k");
Log.i(tag,"系统是否处于低内存运行:"+info.lowMemory);
Log.i(tag,"当系统剩余内存低于"+info.threshold+"时就看成低内存运行");
}
6.媒体文件通知媒体数据库更新;
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment .getExternalStorageDirectory())));
this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
7.禁用硬件加;
也就是在application标签里面添加了一句android:hardwareAccelerated="false" (禁用硬件加速)
8.在同一layout中切换Fragment(不重新加载fragment);
private FragmentTransaction transaction;
private Fragment currentFrag;
/**
* @param displayFrag 要显示的fragment
*
* @author stone
*/
private void display(Fragment displayFrag) {
if (currentFrag != displayFrag) {
transaction = manager.beginTransaction();
if (displayFrag == null) return;
if (displayFrag.isAdded()) {
transaction.show(displayFrag);
} else {
transaction.add(R.id.fl_content, displayFrag);
}
if (currentFrag != null) {
transaction.hide(currentFrag);
}
currentFrag = displayFrag;
transaction.commit();
}
}
/**
* 切换Fragment|不删
* author zx
* version 1.0
* since 2017/4/6 14:14
*/
private void switchFragment(Fragment fragment, String text) {
if (getActivity() == null) {
return;
}
if (getActivity() instanceof HomeActivity) {
HomeActivity fca = (HomeActivity) getActivity();
fca.displayFragment(fragment, text, true);
}
}
9.点击振动;
Vibrator vibrator=(Vibrator) getApplication().getSystemService(Service.VIBRATOR_SERVICE);
vibrator.vibrate(new long[]{0,100}, -1);
10.android 等比例缩放图片;
/**
* 按比例缩放图片
* @param bm
* @param newWidth
* @param newHeight
* @return
*/
public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,true);
return newbm;
}
11.Android系统自带样式Android:theme;
android:theme="@android:style/Theme.Light" 背景白色
android:theme="@android:style/Translucent" 半透明效果
android:theme="@android:style/Theme.Light" 背景为白色
android:theme="@android:style/Theme.NoTitleBar" 不显示应用程序标题栏
android:theme="@android:style/Theme.Wallpaper" 用系统桌面为应用程序背景
android:theme="@android:style/Theme.Black.NoTitleBar" 黑色背景并无标题栏
android:theme="@android:style/Theme.Light.NoTitleBar" 白色背景并无标题栏
android:theme="@android:style/Theme.Dialog" 将一个Activity显示为对话框模式
android:theme="@android:style/Theme.Translucent.NoTitleBar" 半透明无标题栏
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 不显示应用程序标题栏,全屏
android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 白色背景,无标题栏,全屏
android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" 黑色背景,无标题栏,全屏
android:theme="@android:style/Theme.Wallpaper.NoTitleBar" 用系统桌面为应用程序背景且无标题栏
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 半透明效果,无标题栏,全屏
android:theme="@android:style/Theme.Wallpaper.NoTitleBar.Fullscreen" 用系统桌面为应用程序背景,无标题栏,全屏
12.android状态栏透明化【全屏】;
/**
* 支持全屏
* @author zx
* @return void
*/
private void setFullScreen() {
requestWindowFeature(Window.FEATURE_NO_TITLE);
int newVis = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
newVis = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
| View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
this.getWindow().getDecorView().setSystemUiVisibility(newVis);
}
————————————————2016.1.8-new
private View mDecorView;
// 消失导航栏
mDecorView = getWindow().getDecorView();
hideSystemUI();
/**
* 透明导航栏,点击消失
* @author zx
* @return void
*/
private void hideSystemUI() {
mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LOW_PROFILE
| View.SYSTEM_UI_FLAG_IMMERSIVE
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
13.Android 按下home键,程序在后台运行,在launcher里面再点击icon又重新启动了,解决方法;
// Home键不重启
if (!isTaskRoot()) {
finish();
return;
}
14.Android 更新文件到媒体库;
/**
* 更新文件到媒体库
* @author zx
* @return void
*/
private void senderFile(File file) {
final Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
getActivity().sendBroadcast(intent);
}