Android getResources()。getDrawable()已弃用API 22

本文翻译自:Android getResources().getDrawable() deprecated API 22

With new android API 22 getResources().getDrawable() is now deprecated. 使用新的android API 22,现在不建议使用getResources().getDrawable() Now the best approach is to use only getDrawable() . 现在最好的方法是仅使用getDrawable()

What changed? 发生了什么变化?


#1楼

参考:https://stackoom.com/question/1xqtf/Android-getResources-getDrawable-已弃用API


#2楼

Edit: see my blog post on the subject for a more complete explanation 编辑:有关此主题的更多信息 ,请参见我的博客文章


You should use the following code from the support library instead: 您应该改用支持库中的以下代码:

ContextCompat.getDrawable(context, R.drawable.***)

Using this method is equivalent to calling: 使用此方法等效于调用:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    return resources.getDrawable(id, context.getTheme());
} else {
    return resources.getDrawable(id);
}

As of API 21, you should use the getDrawable(int, Theme) method instead of getDrawable(int) , as it allows you to fetch a drawable object associated with a particular resource ID for the given screen density/theme. 从API 21开始,应该使用getDrawable(int, Theme)方法代替getDrawable(int) ,因为它允许您针对给定的屏幕密度/主题获取与特定资源ID关联的可绘制对象。 Calling the deprecated getDrawable(int) method is equivalent to calling getDrawable(int, null) . 调用不推荐使用的getDrawable(int)方法等效于调用getDrawable(int, null)


#3楼

Replace this line : getResources().getDrawable(R.drawable.your_drawable) 替换此行: getResources().getDrawable(R.drawable.your_drawable)

with ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null) ResourcesCompat.getDrawable(getResources(), R.drawable.your_drawable, null)

EDIT 编辑

ResourcesCompat is also deprecated now. 现在不推荐使用ResourcesCompat But you can use this: 但是您可以使用以下命令:

ContextCompat.getDrawable(this, R.drawable.your_drawable) (Here this is the context) ContextCompat.getDrawable(this, R.drawable.your_drawable)下面this是上下文)

for more details follow this link: ContextCompat 有关更多详细信息,请单击此链接: ContextCompat


#4楼

You have some options to handle this deprecation the right (and future proof ) way, depending on which kind of drawable you are loading: 您有一些选择可以以正确的方式(以及将来的证明 )来处理这种弃用,这取决于您要加载的绘图类型:


A) drawables with theme attributes A) 具有主题属性的可绘制对象

ContextCompat.getDrawable(getActivity(), R.drawable.name);

You'll obtain a styled Drawable as your Activity theme instructs. 您将按照Activity主题的指示获得样式化的Drawable。 This is probably what you need. 这可能就是您所需要的。


B) drawables without theme attributes B) 没有主题属性的绘画

ResourcesCompat.getDrawable(getResources(), R.drawable.name, null);

You'll get your unstyled drawable the old way. 您将以旧方式获得未样式化的可绘制对象。 Please note: ResourcesCompat.getDrawable() is not deprecated! 请注意: 推荐使用ResourcesCompat.getDrawable()


EXTRA) drawables with theme attributes from another theme 具有 其他主题的主题属性的EXTRA)可绘制对象

ResourcesCompat.getDrawable(getResources(), R.drawable.name, anotherTheme);

#5楼

getResources().getDrawable() was deprecated in API level 22. Now we must add the theme: 在API级别22中不赞成使用getResources().getDrawable() 。现在,我们必须添加主题:

getDrawable (int id, Resources.Theme theme) (Added in API level 21) getDrawable(int id,Resources.Theme主题) (在API级别21中添加)

This is an example: 这是一个例子:

myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));

This is an example how to validate for later versions: 这是一个如何验证更高版本的示例:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //>= API 21
     myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage, getApplicationContext().getTheme()));
   } else { 
     myImgView.setImageDrawable(getResources().getDrawable(R.drawable.myimage));
}

#6楼

Build.VERSION_CODES.LOLLIPOP should now be changed to BuildVersionCodes.Lollipop ie: Build.VERSION_CODES.LOLLIPOP现在应该更改为BuildVersionCodes.Lollipop,即:

if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop) {
    this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder, Context.Theme);
} else {
    this.Control.Background = this.Resources.GetDrawable(Resource.Drawable.AddBorder);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常抱歉,我之前的回答有误,Android Camera.Parameters确实没有提供setPreviewFlipped()方法来设置预览镜像。如果您需要设置预览镜像效果,可以使用以下两种方法: 1. 使用Matrix类来翻转预览视图 您可以使用Matrix类来翻转预览视图。在setPreviewCallback()方法中,获取相机预览数据的byte[]数组,将其转换为Bitmap对象,使用Matrix类将Bitmap对象水平或垂直翻转,然后将其显示在SurfaceView上。 以下是一个示例代码片段,演示如何使用Matrix类来翻转预览视图: ```java Camera camera = Camera.open(); Camera.Parameters parameters = camera.getParameters(); // 设置预览大小 parameters.setPreviewSize(640, 480); camera.setParameters(parameters); // 绑定SurfaceView并开始预览 SurfaceView surfaceView = findViewById(R.id.surface_view); SurfaceHolder surfaceHolder = surfaceView.getHolder(); camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); // 设置预览回调 camera.setPreviewCallback(new Camera.PreviewCallback() { @Override public void onPreviewFrame(byte[] data, Camera camera) { // 将预览数据转换为Bitmap对象 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); // 创建Matrix对象并翻转Bitmap Matrix matrix = new Matrix(); matrix.preScale(-1, 1); Bitmap flippedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // 显示翻转后的Bitmap surfaceView.draw(new BitmapDrawable(getResources(), flippedBitmap)); } }); ``` 在上面的代码中,我们使用Matrix类的preScale()方法将Bitmap对象水平翻转,然后使用Bitmap.createBitmap()方法创建翻转后的Bitmap对象,并将其显示在SurfaceView上。 2. 使用Camera.CameraInfo类来翻转预览视图 您也可以使用Camera.CameraInfo类的facing属性来翻转预览视图。在Camera.open()之后,您可以调用Camera.CameraInfo类的静态方法getCameraInfo()来获取相机信息,然后使用Camera.Parameters类的setPreviewSize()方法设置预览大小,并根据Camera.CameraInfo类的facing属性设置预览镜像。 以下是一个示例代码片段,演示如何使用Camera.CameraInfo类来翻转预览视图: ```java Camera camera = Camera.open(); Camera.Parameters parameters = camera.getParameters(); // 设置预览大小 parameters.setPreviewSize(640, 480); // 获取相机信息 Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); Camera.getCameraInfo(0, cameraInfo); // 根据相机信息设置预览镜像 if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { parameters.setPreviewSize(640, 480); camera.setDisplayOrientation(180); } camera.setParameters(parameters); // 绑定SurfaceView并开始预览 SurfaceView surfaceView = findViewById(R.id.surface_view); SurfaceHolder surfaceHolder = surfaceView.getHolder(); camera.setPreviewDisplay(surfaceHolder); camera.startPreview(); ``` 在上面的代码中,我们使用Camera.CameraInfo类的getCameraInfo()方法获取相机信息,然后根据相机信息的facing属性设置预览镜像。如果相机是前置摄像头,我们将预览大小设置为640x480,并使用Camera.setDisplayOrientation()方法将预览旋转180度,实现水平翻转的效果。如果相机是后置摄像头,我们不进行任何操作,保持预览不变。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值