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

<android>我遇到的机型适配(安卓开发屏幕适配&rom适配)之Rom篇

2018-01-10 15:55 609 查看
一路走来,都离不开机型适配
Rom篇

提起适配,不得不说安卓的适配太麻烦了,各种机型,各种阉割版本,各种屏幕分辨率,但是做过了很多东西,就发觉适配需要整理个文档记录一下的,不然会忘记,在这里我就把我多年来适配的经验分享给大家,也做一个笔记,方便以后工作中用到。

1.在搞上传头像时,遇到过拍照或获取相册照片时拿到头像旋转270和90度的问题,发生在三星手机上比较多。解决办法判断旋转角度,强制旋转过来:

/**
* 三星手机适配(图片旋转90度问题) ==========找到被旋转角度
* 读取图片属性:旋转的角度 int degree = readPictureDegree(ss);
*
* @return degree旋转的角度
*/
public static int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}

/**
* 三星手机适配(图片旋转90度问题) ==========图片回正
* 读取图片属性:旋转的角度 int degree = readPictureDegree(ss);
*/
public static Bitmap rotaingImageView(int angle, Bitmap bitmap) {
// 旋转图片 动作
Matrix matrix = new Matrix();

matrix.postRotate(angle);
System.out.println("angle2=" + angle);
// 创建新的图片
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizedBitmap;
}


2.在搞动画的时候,遇到过动画变形,若隐若现的效果,肉眼看去像是丢帧。发生在华为手机、乐视2,荣耀8是闪烁效果,最为严重。解决办法,直接更换成加动画,做两张图。

3. N系列  4.4以上和以下,以下虽然说很多公司不再去适配了,但是方法还是要知道的:

3.1 webview套页前端<input>标签配合打开本地相册:

if (isHighV) {  //高版本
List<String> photos = (List<String>) data.getSerializableExtra(GalleryActivity.PHOTOS);
String result = photos.get(0);
if (null == mUploadMessage1)
return;
Uri[] c = new Uri[1];
Uri uri = getUri(result);
c[0] = uri;
mUploadMessage1.onReceiveValue(c);
mUploadMessage1 = null;
return;
} else {  //低版本
List<String> photos = (List<String>) data.getSerializableExtra(GalleryActivity.PHOTOS);
String result = photos.get(0);
if (null == mUploadMessage)
return;
Uri uri = getUri(result);
mUploadMessage.onReceiveValue(uri);
mUploadMessage = null;
return;
}

/**
* 在此处监听网页事件  并打开相册
*/
class MyChromeClient extends WebChromeClient {
public boolean onShowFileChooser(android.webkit.WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
isHighV = true;
mUploadMessage1 = filePathCallback;
GalleryActivity.openActivity(WebViewActivity.this, true, 100);
return true;
}

// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg) {
mUploadMessage = uploadMsg;
isHighV = false;
//打开图库 用的compile 'com.library.tangxiaolv:telegramgallery:1.0.1'框架
GalleryActivity.openActivity(WebViewActivity.this, true, 100);
}

//3.0--版本
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
openFileChooser(uploadMsg);
}

// For Android 4.1
public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {
openFileChooser(uploadMsg);
}

3.2 版本更新时的rom适配问题 ==> 拉起安装界面  N以上
报错log: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageI
解决方案:
        Intent intent = new Intent(Intent.ACTION_VIEW);
//判断是否是AndroidN以及更高的版本
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {  //高版本
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(mContext, "com.xxxxxxx.xxx.fileprovider", file);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {   //低版本
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        mContext.startActivity(intent);

然后配置一下AndroidManifest.xml:

<provider
android:authorities="com.xxxx.xxxx.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"/>
</provider>

在res目录下创建一个xml文件夹,xml文件夹下创建一个filepaths.xml,里面写上如下代码:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path path="Android/data/com.xxx.xxxx/" name="files_root" />
<external-path path="." name="external_storage_root" />
</paths>
注意包名哦,配置完你就发现不会报错问题了。

后续会持续更新!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: