1、給View增加顯示、隱藏動畫,靜態方法,全局調用。
/**
* View顯示底部彈出動畫
* */
public static Animation ShowAnimation(Context context){
Animation anim = AnimationUtils.loadAnimation(context, R.anim.push_bottom_in);
return anim;
}
/**
* View隱藏底部動畫
* */
public static Animation HideAnimation(Context context){
Animation anim = AnimationUtils.loadAnimation(context, R.anim.push_bottom_out);
return anim;
}
調用代碼:
activity_public_delete_hint.startAnimation(BaseMethod.ShowAnimation(context));
activity_public_delete_hint.startAnimation(BaseMethod.HideAnimation(context));
2、獲取屏幕高度等參數:
//獲取狀態欄高度
public static int Statusbar(Activity activity) {
int high = 0;
Rect rectangle = new Rect();
Window window = activity.getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
high = rectangle.top;
return high;
}
//獲取屏幕高度
public static int Shigh(Activity activity) {
int Shigh = 0;
WindowManager windowManager = activity.getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
Shigh = display.getHeight();
return Shigh;
}
//獲取屏幕寬度
public static int Swidth(Activity activity) {
int Swidth = 0;
WindowManager windowManager = activity.getWindow().getWindowManager();
Display display = windowManager.getDefaultDisplay();
Swidth = display.getWidth();
return Swidth;
}
public static void MathScreen(Activity activity) {
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
3、搜索的時候,需要將搜索的參數加入到服務器網址中,通過訪問帶關鍵字的網址得到返回值,這個時候不能直接將文字直接添加到網址中,需將輸入參數進行轉碼再構成網址,轉碼代碼:
/**
* 網址輸入參數轉碼
* */
public static String urlcode(String str) {
String text = null;
try {
text = URLEncoder.encode(str, "utf8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return text;
}
4、像搜索功能,搜索得到一個列表,列表的每個子項是長文字,其中包含了搜索的文字,這時候,搜索文字的顏色應該跟其他字的顏色不一樣,改變一個Textview中部分文字的顏色代碼:
/**
* 改變部分字體顏色
* */
public static void Tvcolor(TextView tv, String str, String tvcolor) {
int fstart = str.indexOf(tvcolor);
int fend = fstart + tvcolor.length();
SpannableStringBuilder style = new SpannableStringBuilder(str);
style.setSpan(new ForegroundColorSpan(Color.parseColor("#f58f2d")), fstart, fend, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
tv.setText(style);
}
5、判斷SDCard是否存在、讀取內存卡路徑
public static String getSDCardPath() {
File sdcardDir = null;
boolean sdcardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
if (sdcardExist) {
sdcardDir = Environment.getExternalStorageDirectory();
}
return sdcardDir.toString();
}
6、將Bitmap保存到本地
public static String path = Environment.getExternalStorageDirectory().getPath();
public static void saveMyBitmap(String bitName, Bitmap mBitmap) {
File f = new File( path + "ico_0.png");
try {
f.createNewFile();
} catch (IOException e) {
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}
7、讀取本地圖片文件,加載到Imageview上,如果本地圖片文件不存在,則在服務器下載
public static String path = Environment.getExternalStorageDirectory().getPath();
public static void readusericon(String url, RoundImageView iv ) {
File file = new File( path +"ico_0.png" );
if (file.exists()) {
Bitmap bm = BitmapFactory.decodeFile( path + "ico_0.png");
iv.setImageBitmap(bm);
} else {
//如果本地圖片文件不存在,則在服務器下載
new NormalLoadPictrue().getPicture(url, iv);
}
}
8、把數組轉換爲一個用逗號分隔的字符串 ,以便於將一組數據傳到服務器:
public static String converToString(String[] ig) {
String str = "";
if (ig != null && ig.length > 0) {
for (int i = 0; i < ig.length; i++) {
str += ig[i] + ",";
}
}
str = str.substring(0, str.length() - 1);
return str;
}
9、把list轉換爲一個用逗號分隔的字符串,以便於將一組數據傳到服務器:
public static String listToString(List list) {
StringBuilder sb = new StringBuilder();
if (list != null && list.size() > 0) {
for (int i = 0; i < list.size(); i++) {
if (i < list.size() - 1) {
sb.append(list.get(i) + ",");
} else {
sb.append(list.get(i));
}
}
}
return sb.toString();
}
10、限制輸入法的Enter換行
public static void limitEnter( EditText et ){
et.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
return (event.getKeyCode() == KeyEvent.KEYCODE_ENTER);
}
});
}
11、將Textview所有字體都換成全角
public static String ToDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i< c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}if (c[i]> 65280&& c[i]< 65375)
c[i] = (char) (c[i] - 65248);
}
return new String(c);
}
12、在內存卡創建文件夾以及二層文件夾,用於緩存目錄:
public static String path = Environment.getExternalStorageDirectory().getPath();
public static void createSDCardDir(){
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
// 創建一個文件夾對象,賦值爲外部存儲器的目錄
//得到一個路徑,內容是sdcard的文件夾路徑和名字
String str = path + "/myfile";
File path1 = new File(str);
if (!path1.exists()) {
//若不存在,創建目錄,可以在應用啓動的時候創建
path1.mkdirs();
}
String sunico= str + "/ico";
File sunpath = new File(sunico);
if (!sunpath.exists()) {
//若不存在,創建目錄,可以在應用啓動的時候創建
sunpath.mkdirs();
}
}
else{
return;
}
}
13、刪除文件、輸入參數是文件路徑
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.isFile() && file.exists()) {
return file.delete();
}
return false;
}
14、解決viewpager與SwipeRefreshLayout的滑動衝突
public static void setViewpagerSRL(ViewPager vp , final SwipeRefreshLayout srl){
vp.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_MOVE:
srl.setEnabled(false);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
srl.setEnabled(true);
break;
}
return false;
}
});
}