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

android 开发中常见问题合集(持续更新)

2016-03-13 16:39 525 查看
ps:有遇到新的问题,我会继续在这个博客上更新我的思路和解决方法。希望可以帮到大家

7.微信分享问题

未签名的包可以正常分享,打了签名的包却不能分享

原因1.微信开发者网站里面配置的签名和签名包的签名不一致,这里不细说,请认真比对一下

        2.混淆的问题:文档上面没有说,但是我添加了下面的混淆后就可以正常分享了

         -keep class com.tencent.mm.sdk.openapi.WXMediaMessage {*;}
-keep class com.tencent.mm.sdk.openapi.** implements com.tencent.mm.sdk.openapi.WXMediaMessage$IMediaObject {*;}
-dontwarn com.tencent.mm.**
-keep class com.tencent.mm.**{*;}
6.fresco 加载本地图片的问题

将我们本地的地址通过Uri.fromFile来处理而不是用以前使用的Url.parse()来处理

 例如:DraweeUtils.showThumb(Uri.fromFile(new File(itemData.getCommonImageUrl())), cache.localPreview);

5.关于.9图的Drawabel的转化异常

现在的app经常要应用.9图,但是我们拿到.9图的Drawable的时候要转化为bitmap等操作的时候经常活报错,这边就给一个关于.9图的转化方法

private Bitmap drawable2Bitmap(Drawable drawable) {
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof NinePatchDrawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
} else {
return null;
}
}


4.关于动态设置背景的颜色和背景的圆角的问题

--最近公司的一个需求是根据后台传递过来的颜色设置背景的颜色,如果我们使用setBackgroundResource(R.drawable.xxx),但是如果是资源文件设置background的话里面的颜色是没法动态设置进去的,所以就需要我们动态的去设置背景颜色和背景的圆角

--以下是主要的代码:

GradientDrawable drawable = (GradientDrawable) mContext.getResources().getDrawable(R.drawable.item_category_bg);
drawable.setCornerRadius(Util.dip2px(mContext,10));
drawable.setColor(Color.parseColor(categoryBeens.get(position).getBgColor()));
bgColor.setBackgroundDrawable(drawable);
ps:item_category_bg就是自定义的背景xml,我的是很简单的xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>


3.获取输入法弹框的高度等问题

--解决思路:通过根视图,拿到根视图的高度, 获取root在窗体的不可视区域高度(被其他View遮挡的区域高度),就使我们要的输入法的高度了

final RelativeLayout root= (RelativeLayout) findViewById(R.id.layout_chat);//layout_chat是最外层的布局id
final RelativeLayout rl_input= (RelativeLayout) findViewById(R.id.rl_input);

if(Constants.isMonitoring&&Util.isKitKat()){
root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//获取root在窗体的可视区域
root.getWindowVisibleDisplayFrame(rect);
//获取root在窗体的不可视区域高度(被其他View遮挡的区域高度)
int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;
if (rootInvisibleHeight > 100) {
if (Util.isKitKat()) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(rl_input.getLayoutParams());
lp.setMargins(0, 0, 0, rootInvisibleHeight);
rl_input.setLayoutParams(lp);
mLayoutManager.scrollToPosition(mAdapter.getItemCount() - 1);
}
} else {
if (Util.isKitKat()) {
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(rl_input.getLayoutParams());
lp.setMargins(0, 0, 0, 0);
rl_input.setLayoutParams(lp);
}
}
root.getViewTreeObserver().removeGlobalOnLayoutListener(null);
}
});
}


2.图片同比例放大的问题

--解决思路:自定义一个imageview,在ondraw方法获取到图片,并且得到图片宽高,然后根据图片宽高比和屏幕宽高比获得要生成的新图的宽高比,最后生成图片

@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
try{
Drawable drawable = getDrawable();
if (null != drawable) {
float height = ScreenUtil.getCurrentScreenHeight1(getContext());
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
int ImageHei=bitmap.getHeight();
int ImageWid=bitmap.getWidth();
float heigh= height/3;
float scale=(float)ImageHei/heigh;
int width= (int) (ImageWid/scale);

bitmap=zoomDrawable((BitmapDrawable) drawable,heigh);
canvas.drawBitmap(bitmap, 0, 0, new Paint());
}
}catch (Exception e){
e.printStackTrace();
}
}
   ---出现的问题是:图片宽度很大的话会被压缩

  最后的解决方案是:

   

@SuppressLint("NewApi")
@Override
protected void onDraw(Canvas canvas) {
try{
Drawable drawable = getDrawable();
if (null != drawable) {
float height = ScreenUtil.getCurrentScreenHeight1(getContext());
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
int ImageHei=bitmap.getHeight();
int ImageWid=bitmap.getWidth();
float heigh= height/3;
float scale=(float)ImageHei/heigh;
int width= (int) (ImageWid/scale);
//-----------------------------------------------------------------------------------
ViewGroup.LayoutParams layoutParams = getLayoutParams();
layoutParams.width=width;
layoutParams.height= (int) heigh;
<pre name="code" class="java">//-----------------------------------------------------------------------------------
bitmap=zoomDrawable((BitmapDrawable) drawable,heigh); canvas.drawBitmap(bitmap, 0, 0, new Paint()); } }catch (Exception e){ e.printStackTrace(); } }



然后在imageloader的display方法中调用;

ImageLoader.getInstance().displayImage(regionList.get(position), imageView,
ImageLoaderManager.getDisplayImageOptions(R.drawable.bg_normal_pic), new
SimpleImageLoadingListener() {
@Override
public void onLoadingStarted(String imageUri, View view) {
loading.setVisibility(View.VISIBLE);
}

@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
String message = null;
switch (failReason.getType()) {     // 获取图片失败类型
case IO_ERROR:              // 文件I/O错误
message = "Input/Output error";
break;
case DECODING_ERROR:        // 解码错误
message = "Image can't be decoded";
break;
case NETWORK_DENIED:        // 网络延迟
message = "Downloads are denied";
break;
case OUT_OF_MEMORY:         // 内存不足
message = "Out Of Memory error";
break;
case UNKNOWN:               // 原因不明
message = "Unknown error";
break;
}
loading.setVisibility(View.GONE);
imageView.setBackgroundResource(R.drawable.bg_normal_pic);
}

@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if(regionList.get(position).equals(imageUri)){
float height = ScreenUtil.getCurrentScreenHeight(mContext);
int ImageHei=loadedImage.getHeight();
int ImageWid=loadedImage.getWidth();
float heigh= height/3;
float scale=(float)ImageHei/heigh;
int width= (int) (ImageWid/scale);
//                                    Log.i("onLoadingComplete", "onLoadingComplete:" + width);
ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
layoutParams.width=width;
layoutParams.height=imageHei;
imageView.setLayoutParams(layoutParams);
// imageView.setImageBitmap(loadedImage);
loading.setVisibility(View.GONE);       // 不显示圆形进度条
}
}
});


1.内存溢出的问题

--解决思路:拍照后生成图片的时候直接压缩图片,减少内存(错误思路:先生成好图片,然后再去压缩,这样效果不好,经常没办法解决图片内存溢出的问题)

// 解决加载图片 内存溢出的问题  

// Options 只保存图片尺寸大小,不保存图片到内存  

BitmapFactory.Options opts = new BitmapFactory.Options();  

// 缩放的比例,缩放是很难按准备的比例进行缩放的,其值表明缩放的倍数,SDK中建议其值是2的指数值,值越大会导致图片不清晰  

opts.inSampleSize = 4;  

Bitmap bmp = null;  

bmp = BitmapFactory.decodeResource(getResources(), mImageIds[position],  

                opts);  

// 回收  

bmp.recycle();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: