创建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, ...
随机推荐
- Vue、webpack中默认的config.js、index.js 配置详情
在vue.js 框架搭建好后,其vue-cli 自动构建的目录里面相关环境变量及其基本变量配置,如下代码所示: module.exports = { build: { index: path.reso ...
- go语言-数据类型及类型之间转换
数据类型分类 一.数据类型-基本数据类型 1.整数型(int.有符号(int8/1字节.int16/2字节.int32/4字节.int64/8字节).无符号(uint.uint8.uint16.uin ...
- 2019年java技术大盘点
福州SEO:2019年互联网企业在Java开发中有哪些主流.热门的IT技术呢,下面让我们来看一下. 微服务技术 微服务架构主要有:Spring Cloud. Dubbo. Dubbox等,以 Dubb ...
- LightOJ - 1179-Josephus Problem(约瑟夫环)
链接: https://vjudge.net/problem/LightOJ-1179 题意: The historian Flavius Josephus relates how, in the R ...
- DOM设置css样式
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- MFC的PictureControl控件上展示Halcon的窗体
自定义函数:id是MFC窗体上的PictureControl控件的ID,此函数返回halcon的HWindow对象. HTuple initPicControl(int id); HTuple CHa ...
- &和&&,|和||的用法区别
&和&&的区别是,&会执行两边,不管第一个是否成立&&只会执行一边,如果第一个条件为假,则不会走第二个条件举例public class Test2{ p ...
- java 数组对象的应用
public class Student 2 { 3 // 成员变量 4 private String name; 5 private int age; 6 7 // 构造方法 8 public St ...
- sql server 数据类型转换
--这是显示转换类型,将字符串转成整形SELECT CAST('1' AS int),CONVERT(int,'1')--SqlServer也会隐式转换,如:用字符串乘以一个整形,--整形优先级要大, ...
- jsp中submit的连接属性是什么?
把注册的按钮改成button:<form action="dologin.jsp" method="post">用户<input type=& ...