创建Bitmap之Bitmap静态方法使用示例
package com.loaderman.customviewdemo; import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //创建白色透明渐变图像
findViewById(R.id.bitmap_blank_gradient).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, BmpBlankGradientActivity.class));
}
}); //裁剪图像
findViewById(R.id.bitmap_crop).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cutImage();
}
}); //剪图像并用Matrix
findViewById(R.id.bitmap_crop_matrix).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cutImageMatrix();
}
}); //指定色彩创建图像
findViewById(R.id.bitmap_colors).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createBmpByColors();
}
}); //createScaledBitmap
findViewById(R.id.bitmap_scaled).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createScaledBitmap();
}
});
} //裁剪图像
private void cutImage() { Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
Bitmap cutedBmp = Bitmap.createBitmap(src, src.getWidth() / 3, src.getHeight() / 3, src.getWidth() / 3, src
.getHeight() / 3); ImageView iv = (ImageView) findViewById(R.id.bmp_img);
iv.setImageBitmap(cutedBmp);
} //裁剪图像并用Matrix
private void cutImageMatrix() {
Matrix matrix = new Matrix();
matrix.setScale(2, 1); Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.dog);
Bitmap cutedBmp = Bitmap.createBitmap(src, src.getWidth() / 3, src.getHeight() / 3, src.getWidth() / 3, src
.getHeight() / 3, matrix, true); ImageView iv = (ImageView) findViewById(R.id.bmp_img2);
iv.setImageBitmap(cutedBmp);
} //指定色彩创建图像
private void createBmpByColors() {
int width = 300, height = 200;
int[] colors = initColors(width, height);
Bitmap bmp = Bitmap.createBitmap(colors, width, height, Bitmap.Config.ARGB_8888); ImageView iv = (ImageView) findViewById(R.id.bmp_img);
iv.setImageBitmap(bmp);
} private int[] initColors(int width, int height) {
int[] colors = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int r = x * 255 / (width - 1);
int g = y * 255 / (width - 1);
int b = 255 - Math.min(r, g);
int a = Math.max(r, g);
colors[y * width + x] = Color.argb(a, r, g, b);
}
}
return colors;
}
//缩放bitmap
private void createScaledBitmap() {
try {
Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.scenery);
Bitmap bitmap = Bitmap.createScaledBitmap(src, 300, 200, true); ImageView iv = (ImageView) findViewById(R.id.bmp_img);
iv.setImageBitmap(bitmap);
}catch (OutOfMemoryError error){
error.printStackTrace();
}
}
}
package com.loaderman.customviewdemo; import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.View; public class LinearGradientView extends View {
private Bitmap mDestBmp;
private Paint mPaint;
public LinearGradientView(Context context) {
super(context);
init();
} public LinearGradientView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
} public LinearGradientView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
} private void init(){
mPaint = new Paint(); int width = 500;
int height = 300;
mDestBmp = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(mDestBmp);
Paint paint = new Paint();
LinearGradient linearGradient = new LinearGradient(width/2,0,width/2,height,0xffffffff,0x00ffffff, Shader.TileMode.CLAMP);
paint.setShader(linearGradient);
canvas.drawRect(0,0,width,height,paint);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas); canvas.drawBitmap(mDestBmp,0,0,mPaint); mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(5);
canvas.drawRect(0,0,mDestBmp.getWidth(),mDestBmp.getHeight(),mPaint);
}
}
BmpBlankGradientActivity布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context="com.loaderman.customviewdemo.BmpBlankGradientActivity">
<com.loaderman.customviewdemo.LinearGradientView
android:layout_width="match_parent"
android:layout_height="match_parent"/> </LinearLayout>
创建Bitmap之Bitmap静态方法使用示例的更多相关文章
- java 添加一个线程、创建响应的用户界面 。 演示示例代码
javajava 添加一个线程.创建响应的用户界面 . 演示示例代码 来自thinking in java 4 21章 部分的代码 夹21.2.11 thinking in java 4免费下载: ...
- Android -- 重置Bitmap大小&&Bitmap转角度
重置Bitmap大小 Bitmap bitMap = ...
- 创建一个RAS 非对称 公私密钥示例
static void Main(string[] args) { RSAParameters pub; RSAParameters priv; using (var rsa = new RSACry ...
- SQL Server 中创建数据库、更改主文件组示例
以下示例在 SQL Server 实例上创建了一个数据库.该数据库包括一个主数据文件.一个用户定义文件组和一个日志文件.主数据文件在主文件组中,而用户定义文件组包含两个次要数据文件.ALTER DAT ...
- Robot Framework - 4 - 创建和扩展测试库的示例
创建和扩展Library的示例 示例:Check status on Linux OS 创建与使用library的基本步骤: 1--- library实现的内容和实现的方式 ...
- SpringBoot项目创建与第一个SSM项目示例
本节介绍SpringBoot创建第一个示例SSM项目的完整过程,使用工具STS,与IDEA操作基本类似. 示例代码在:https://github.com/laolunsi/spring-boot-e ...
- Bitmap: 使用Bitmap作为绘图缓冲时设置抗锯齿
android上绘图时常用的抗锯齿方法是: paint.setAntiAlias(true); 但是在以Bitmap作为绘图缓冲绘制时,绘制出来的Bitmap可能仍然有锯齿,此时可以在绘制开始前加上下 ...
- Angular CLI 创建你的第一个 Angular 示例程序
第一步:安装 Angular CLI 你要使用 Angular CLI 来创建项目.创建应用和库代码,并执行多种开发任务,比如测试.打包和发布. 全局安装 Angular CLI. 要想使用 npm ...
- python从命令窗口启动脚本 创建并写入内容到文件示例
写入到文件示例: #!/usr/bin/env python3 from math import exp, log, sqrt import re from datetime import date, ...
随机推荐
- Scyther-Compromise 协议形式化安全分析如何改进协议
1.最终的目的是如何将协议的不安全因素进行改进,提升安全性能.对协议中有关的加密和认证的过程进行形式化分析验证的时候通过添加敌手模型的(DY模型和eCK强安全模型),接受者和发送者之间的通信过程可能存 ...
- linux安装zookeeper,安装zkui,zookeeper可视化
系统要求 支持的平台 ZooKeeper由多个组件组成.某些组件得到广泛支持,其他组件仅在较小的平台上受支持. 客户端是Java客户端库,应用程序使用它连接到ZooKeeper集合. Server是在 ...
- centos8 安装 mongodb 4.2 (使用yum)
1.制作 repo 文件 参考 mongodb 官方的安装文档,使用下面的脚本制作Yum库安装mongodb4.2,但安装过程提示 "Failed to synchronize cache ...
- 用BCB 画 Code128 B模式条码
//--------------------------------------------------------------------------- #include <vcl.h> ...
- 混合应用 微信登录授权 微信登录认证失败 ios PGWXAPI错误-1 code:-100 / 安卓 message:invalid appsecret innerCode:40125
最近项目需要做微信登录,于是利用HTML5+ API Reference的OAuth模块管理客户端的用户登录授权验证功能,允许应用访问第三方平台的资源.(链接:https://www.dcloud.i ...
- framebufferfetch in mali multiple render targets mrt
gl_LastFragColorARM https://www.khronos.org/registry/OpenGL/extensions/ARM/ARM_shader_framebuffer_fe ...
- [ZJOI2009] 硬币游戏(找规律)
题目 洛谷传送门 题解 把1/21/21/2转化成0/10/10/1,所以直接可以异或. 对于长度为nnn的0/10/10/1数列,发现每变换2k(k>1)2^k(k>1)2k(k> ...
- VMWare虚拟机中网络连接类型对比
1.NAT NAT:Network Address Translation,网络地址转换:虚拟机的网卡连接到宿主的 VMnet8 上 虚拟机与主机的关系:只能单向访问,虚拟机可以通过网络访问到主机,主 ...
- SQL:SQL Broker
-- ============================================= --启用 Broker USE master; GO ALTER DATABASE DEV_AMS S ...
- Greenplum 函数 gp_dist_random
转载自:https://yq.aliyun.com/articles/7593 函数作用: gp_dist_random('gp_id')本质上就是在所有节点查询gp_id, gp_dist_rand ...