Advertisement

android图片资源bitmap,Android 图片Bitmap,drawable,res资源图片之间转换

阅读量:

一、知识介绍

①res资源图片是放在项目res文件下的资源图片

②Bitmap数据格式通常以BMP为文件扩展名。该格式通过编码器将其转换为其他颜色深度的图像形式如RGB565、RGB8888等。基于逐像素渲染的对象具有较快的渲染速度,在视觉效果上表现良好。然而该方法存在明显的局限性,在压缩率上表现不够理想。

Drawable是一种通用的图形对象,在其内部可以包含常用的图像类型:GIF PNG JPG以及BMP。它还支持一系列复杂的可视化功能:渐变与图形

85916a18aca2544ea7a547d8a4cea5b2.png

二、项目案例

【步骤】

①将图片放入res/drawable文件夹中,这里面的图片属于res资源图片

②将图片处理定义成工具类,方便使用,也可以不这么做。

③点击按钮,获取图片,显示出来。

【项目结构】

bbeacc23149a30b91ccb858c94034282.png

【ImgHelper】

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.PixelFormat;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

public class ImgHelper {

在Android平台上开发时,请您使用公共静态方法来获取Bitmap形式资源。该方法可接受Context类型的上下文参数以及整数类型的资源标识符作为输入参数。

return BitmapFactory.decodeResource(context.getResources(),resId);

}

public static method retrievableElementFromResources(Context contextObject, int resId)

return context.getResources().getDrawable(resId);

}

public static Drawable getDrawableFormBitmap(Context context, Bitmap bitmap)是一种方法;该方法根据上下文获取指定位图,并生成基于像素的形式;用于图形处理或图像渲染的应用场景中;

return new BitmapDrawable(context.getResources(),bitmap);

}

public static Bitmap retrieve the bitmap representation from a Drawable object

Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),

The method retrieves the intrinsic height and opacity properties of a Drawable object, comparing them to determine if they differ from the OPAQUE pixel format.

?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(bitmap);

drawable.setBounds(0, 0, drawable.naturalWidth, drawable.naturalHeight);

//设置绘画的边界,此处表示完整绘制

drawable.draw(canvas);

return bitmap;

}

}

提示

操作指南

操作指南

【layout_main】

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:id="@+id/btnBitmapFormRes"

android:text="Bitmap form res"

android:layout_width="match_parent"

android:layout_height="wrap_content" />

android:id="@+id/iv"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginEnd="8dp"

android:layout_marginLeft="8dp"

android:layout_marginRight="8dp"

android:layout_marginStart="8dp"

android:layout_marginTop="8dp"

app:layout_constraintEnd_toEndOf="parent"

app:layout_constraintStart_toStartOf="parent"

app:layout_constraintTop_toBottomOf="@+id/btnBitmapFormRes" />

【提示】可以看到这里ImageView没有设置图片

【Main_Activity】

import android.graphics.Bitmap;

import android.graphics.drawable.Drawable;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import com.example.administrator.myapplication.utils.ImgHelper;

public class MainActivity extends AppCompatActivity {

Button btnBitmapFormRes;

ImageView iv;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnBitmapFormRes = findViewById(R.id.btnBitmapFormRes);

iv = findViewById(R.id.iv);

btnBitmapFormRes.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Bitmap bitmapFormResources = ImgHelper.getBitmapFormResources(MainActivity.this, R.drawable.img1);

// iv.setImageBitmap(bitmapFormResources); //资源图片转BitMap

从资源中获取的这个活动对应的drawable

// iv.setImageDrawable(drawableFromResources); //资源图片转drawable

Bitmap bitmapFormDrawable = ImgHelper.retrievebitmapFormDrawable(MainActivity.this, theGivenResource);

iv.setImageBitmap(bitmapFormDrawable); drawable转BitMap

The Drawable object drawbleFormBitmap is obtained by calling ImgHelper's getDrawableFormBitmap method with MainActivity.this and bitmapFormResources as parameters.

// iv.setImageDrawable(drawbleFormBitmap); //BitMap转drawable

}

});

}

}

提示

提示

提示

【效果】点击按钮后都将显示如下效果

f4d2e1095d0d38aefbcfc6508a4ea14f.png

Android中 Bitmap Drawable Paint的获取、转换以及使用

比如在Drawable类中有多个连续的图片文件如img_0.png img_1.png img_2.png等其中包含img_0.png img_1.png img_2.png等文件名为了动态获取这些图片请参考使用'r.drawable.img_x'这一机制

Android中Bitmap, Drawable, Byte,ID之间的转化

Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap经过转换生成byte数据,并将该数据赋值给ByteArray对象。2. 可能还需要考虑与ID之间的相互转换

Android中Bitmap, Drawable, Byte之间的转化

1. Convert the Bitmap into a byte type operation: create and return a ByteArrayOutputStream object.
Then perform compression on the bitmap.

WPF调用图片路径,或资源图片

项目中图片的加载采用了WPF中的统一资源标识(URI)系统进行管理与引用。其中最常使用的方式是通过URI来获取并处理图像内容。URI表达式的通用格式通常是协议符号加上授权信息,并附加必要的参数和属性以完成特定的操作功能。

Android 控件背景选择图片还是drawable XML资源

决定一个控件是否应使用Drawable XML进行渲染需综合以下几点考量:该应用是否有必要支持多种分辨率该应用是否存在缩减尺寸的需求图案设计是否较为简单图案设计是否允许自由缩放;以及设计开发团队的工作量如何等多方面因素共同决定

Android基础操作指南:第9步使用BitmapFactory.Options对图像资源进行缩放

我们知道,开发的应用程序都面临着一定的内存占用限制.当应用程序过度消耗内存时,就容易引发OOM(OutOfMemory)异常.因此,在展示高分辨率图像时,请务必先对图像进行压缩处理,并确保压缩后的图像尺寸与用于显示的图像尺寸相匹配。

Android 将drawable下的图片转换成bitmap、Drawable

将drawable层级下的图片实现为bitmap形式,并获取其对应的Bitmap对象。Bitmap bitmap = getResources().decodeResource(getResources(), R.drawable.xxx ...)

android将drawable下的图片转换成bitmap

将位于drawable资源中的图片解码为bitmap格式 1. Bitmap bitmap = getResource...

Android 界面效果30

本文主要讨论了ImageSwitcher与Gallery组件在展示SD卡资源图片方面的具体应用。大多数读者可能已经熟悉与此相关的API演示案例。然而,在API演示中所展示的例子主要用于指导工程人员进行绘图操作。

随机推荐

.NET Core中的认证管理解析

在新建一个.NET Core的Web项目时选择"使用个人用户账户"即可自动生成一个带有用户管理和权限控制功能的项目,并且无需额外准备即可配置好用户注册、登录等主要功能页面;此外还可以开发多个功能模块以满足不同需求

Mysql在大型网站的应用架构演变

本文为原创文章,请在转载时注明转自http://www.cnblogs.com/Creator/及其详细链接地址。如需转载,请注明出处。本文已被广泛转载,并已获得包括推荐以及《码农周刊》在内的多个平台的认可与分享。

iOS 通讯录操作

转至:http://superuna.blog.51cto.com/4192682/982938 //新增联系人 -(void)AddPeople { //获取本地通讯录项 ...

转:JAVA强制类型转换

object对象转换为String的一些总结 ----------------------------------------------------------------------------- ...

bzoj 1878 [SDOI2009]HH的项链(离线处理+BIT)

Description HH拥有一条由多种美丽贝壳制成的独特首饰.HH坚信不同种类的贝类能带来好运;因此,在每次散步后他常会随手取出几颗贝类沉思其象征意义.HH不断收藏新鲜贝类;由此,他的首饰日益丰富.

你真正的了解Ajax?Ajax技术简述

Ajax技术即为当下借助JavaScript脚本所能使用的各种技术的集合体. Ajax并未创造任何新的具体技术,其使用的大部分技术和方法早在很久以前就已经存在,然而Ajax以其独特的方式进行 ...

Android学习中R文件中途消失

在建立新工程时,R.java文件原本似乎是存在的,但不知为何在开始操作后就不见了踪影????于是我登录互联网搜索相关信息,发现:原来这些常见的解决方法均已尝试过

EntityFramework连接SQLite

EF展现出强大的能力,然而在 SQLite 中不支持 CodeFirst 模式(在使用前需要预先设计好数据库表结构),尽管如此但在对 SQLite 执行数据操作时依然非常便捷. 接下来请使用 SQLiteManager随意创建一个数据库实例以及一张表:

Centos7下安装php7

采用编译器方式进行安装。
下载所需软件包。
使用wget获取:$ wget http://us2.php.net/distributions/php-7.0.2.tar.gz
然后进行解压。

my golib:db query Result

go提供了一个统一管理数据库SQL接口的完整方案,任何第三方都可以开发相应的驱动程序以访问相关数据库.例如,我们项目中使用了Go-MySQL-Driver. go提供了一套完善的机制来处理 ...

全部评论 (0)

还没有任何评论哟~