15.studio导入项目时一直在building的解决方案
这是因为待导入项目的gradle版本跟本地的不一致导致,studio会主动去下载对应版本,如果被墙肯定是一直在下载中,知道到下载完成
这里有个简单的方法,直接替换项目gradle目录下的distributionUrl=https://services.gradle.org/distributions/gradle-2.14.1-all.zip为本地项目对应目录下文件
16. Studio快捷键,实例化一个对象,可以直接在对象名后.var,快速生成前半部分,例如getArgument().var 之后的结果为Bundle arguments = getArguments();
非空判断,在所需要判断的值后面.nn 或者.notnull都可以生成if(*!=null){}
接收方法返回值,可以用 方法名.field,例如 arguments.getString(“KEY”).field 结果为 mData//自定义的变量名 = arguments.getString(DATA_KEY);
<meta-data android:name="UMENG_CHANNEL" android:value="wandoujia" />
首先你必须在AndroidManifest.xml中的meta-data修改以下的样子:
<meta-data
android:name="UMENG_CHANNEL"
android:value="${UMENG_CHANNEL_VALUE}" />
其中${UMENG_CHANNEL_VALUE}中的值就是你在gradle中自定义配置的值。
build.gradle文件,写到android节点里,就利用productFlavors这样写:
productFlavors {
wandoujia {}
baidu {}
c360 {}
uc {}
productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] } }
其中name的值对相对应各个productFlavors的选项值,这样就达到自动替换渠道值的目的了。
这样生成apk时,选择相应的Flavors来生成指定渠道的包就可以了,而且生成的apk会自动帮你加上相应渠道的后缀,非常方便和直观。大家可以自己反编译验证。
一键打包参考此链接http://www.cnblogs.com/0616--ataozhijia/p/4203997.html
18.压缩图片
//根据显示的宽和高对图片进行压缩
private Bitmap decodeSampleBitmapFromPath(String path, int width, int height) {
//获取图片的宽和高,并不把图片加载到内存中
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
options.inSampleSize = caculateInSampleSize(options, width, height);
options.inJustDecodeBounds = false; //把图片加载到内存中
Bitmap bitmap = BitmapFactory.decodeFile(path, options);//已经进行压缩
return bitmap;
}
private int caculateInSampleSize(Options options, int reqWidth, int reqHeight) {
int width = options.outWidth;
int height = options.outHeight;
int inSampleSize = 1;
if(width > reqWidth || height > reqHeight) {
int widthRadio = Math.round(1.0f * width / reqWidth);
int heightRadio = Math.round(1.0f * width / reqHeight);
inSampleSize = Math.max(widthRadio, heightRadio);
}
return inSampleSize;
}
19.打包后自定义应用的名称,粘贴到build.gradle的根节点
//打包后应用名称
applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile
def fileName
if (outputFile != null && outputFile.name.endsWith('.apk')) {
if (variant.buildType.name.equals('release')) {
fileName = "ONLINE_${getTime()}.apk"
} else if (variant.buildType.name.equals('debug')) {
fileName = "TEST_${getTime()}.apk"
}
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
def getTime() {
return new Date().format("yyyyMMdd_hhmm", TimeZone.getDefault());
}
20.获取manifest.xml中meta-data的键值
appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(),PackageManager.GET_META_DATA);
channel = appInfo.metaData.getInt("MarketID")+"";//还有getString方法,如果是纯数字,必须用getInt否则出错
21.TextView设置下划线
agree.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG ); //下划线
22.对集合对象按属性排序,超简单的方法,只需要传入bean对象,利用的集合工具类的自带方法
Collections.sort(vipBean.getData(), new Comparator<VipBean.DataBean>() {
@Override
public int compare(VipBean.DataBean dataBean, VipBean.DataBean t1) {
if (Integer.parseInt(dataBean.getAnnouncePrice())>Integer.parseInt(t1.getAnnouncePrice())){
return 1;
}
if (Integer.parseInt(dataBean.getAnnouncePrice())==Integer.parseInt(t1.getAnnouncePrice())){
return 0;
}
return -1;
}
});
23.将bitmap处理为圆角并返回
public static Bitmap GetRoundedCornerBitmap(Bitmap bitmap) {
try {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight()));
final float roundPx = 14;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(Color.BLACK);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
final Rect src = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
canvas.drawBitmap(bitmap, src, rect, paint);
return output;
} catch (Exception e) {
return bitmap;
}
}
配合Glide的方法,可以将网络读取的图片资源处理后设置到imageview里,无需自定义控件了
Glide.with(context).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageview.setImageBitmap(GetRoundedCornerBitmap(resource));
}
});
24.将一个bitmap水平反转
Bitmap convert(Bitmap a)
{
int w = a.getWidth();
int h = a.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas(newb);
Matrix m = new Matrix();
m.postScale(1, -1); //镜像水平翻转
//m.postScale(-1, 1); //镜像垂直翻转
m.postRotate(-180); //旋转-180度
Bitmap new2 = Bitmap.createBitmap(a, 0, 0, w, h, m, true);
cv.drawBitmap(new2, new Rect(0, 0, new2.getWidth(), new2.getHeight()),new Rect(0, 0, w, h), null);
return newb;
}
25.验证邮箱的正则表达式
/^[a-zA-Z0-9#_~!$&'()*+,;=:."(),:;<>@\[\]\\]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*$/
26.Fragment保存和恢复状态
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (fragment.isAdded()) {
getSupportFragmentManager().putFragment(outState, "detailFragment", fragment);
}
}
//初始化view时先判断再恢复,在setContentView()之后写
if (savedInstanceState != null) {
fragment = (DetailFragment) getSupportFragmentManager().getFragment(savedInstanceState,"detailFragment");
} else {
fragment = new DetailFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
27.一个判空的引用类
import static com.google.common.base.Preconditions.checkNotNull;