您的位置:首页 > 移动开发 > Android开发

Android 开发的几个代码小工具

2016-05-19 00:28 549 查看
package a.baozouptu.tools;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

/**
* Created by Administrator on 2016/5/19.
*/
public class Util {
public static int dp2Px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}

public static int px2Dp(Context context, float px) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (px / scale + 0.5f);
}

/**
* 在mainifest
4000
中使用android:name=".MyApplication",系统将会创建myapplication替代一般的application
*/
public static class MyApplication extends Application {
private static MyApplication mcontext;

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
mcontext = this;
}

public static Context getAppContext() {
return mcontext;
}
}
public static class P {
public static void le(Object s) {
Log.e(s.toString(), "------");
}

/**
* @param s1 便于输出产生log的内和位置
* @param s2
*/
public static void le(Class s1, Object s2) {
Log.e(s1.getSimpleName(), s2.toString());
}

public static void le(Object s1, Object s2) {
Log.e(s1.toString(), s2.toString());
}

public void lgd(String s) {
Log.d(s, "------");
}

public void lgd(String s1, String s2) {
Log.d(s1, s2);
}
}

/**
* 只是测试时方便写代码的,正式的还是正式的书写
*/
public static class T {
/**
* 默认长的,系统context不为空
*
* @param s
*/
void make(Object s) {
if (MyApplication.getAppContext() != null)
make(MyApplication.getAppContext(), s);
else
P.le("全局的context不存在");
}

public static void make(Context context, Object s) {
Toast.makeText(context, s.toString(), Toast.LENGTH_LONG).show();
}
}

/**
* Created by Administrator on 2016/5/8.
*/
public static class DoubleClick {
public static long lastTime=-1;
public static boolean isDoubleClick(){
long curTime=System.currentTimeMillis();
if(curTime-lastTime<200) {
lastTime=curTime;
return true;
}
else {
lastTime=curTime;
return false;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息