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静态方法使用示例的更多相关文章

  1. java 添加一个线程、创建响应的用户界面 。 演示示例代码

    javajava 添加一个线程.创建响应的用户界面 . 演示示例代码 来自thinking in java 4 21章  部分的代码  夹21.2.11 thinking in java 4免费下载: ...

  2. Android -- 重置Bitmap大小&&Bitmap转角度

    重置Bitmap大小                                                                           Bitmap bitMap = ...

  3. 创建一个RAS 非对称 公私密钥示例

    static void Main(string[] args) { RSAParameters pub; RSAParameters priv; using (var rsa = new RSACry ...

  4. SQL Server 中创建数据库、更改主文件组示例

    以下示例在 SQL Server 实例上创建了一个数据库.该数据库包括一个主数据文件.一个用户定义文件组和一个日志文件.主数据文件在主文件组中,而用户定义文件组包含两个次要数据文件.ALTER DAT ...

  5. Robot Framework - 4 - 创建和扩展测试库的示例

    创建和扩展Library的示例 示例:Check status on Linux OS 创建与使用library的基本步骤:           1--- library实现的内容和实现的方式     ...

  6. SpringBoot项目创建与第一个SSM项目示例

    本节介绍SpringBoot创建第一个示例SSM项目的完整过程,使用工具STS,与IDEA操作基本类似. 示例代码在:https://github.com/laolunsi/spring-boot-e ...

  7. Bitmap: 使用Bitmap作为绘图缓冲时设置抗锯齿

    android上绘图时常用的抗锯齿方法是: paint.setAntiAlias(true); 但是在以Bitmap作为绘图缓冲绘制时,绘制出来的Bitmap可能仍然有锯齿,此时可以在绘制开始前加上下 ...

  8. Angular CLI 创建你的第一个 Angular 示例程序

    第一步:安装 Angular CLI 你要使用 Angular CLI 来创建项目.创建应用和库代码,并执行多种开发任务,比如测试.打包和发布. 全局安装 Angular CLI. 要想使用 npm  ...

  9. python从命令窗口启动脚本 创建并写入内容到文件示例

    写入到文件示例: #!/usr/bin/env python3 from math import exp, log, sqrt import re from datetime import date, ...

随机推荐

  1. Python语言程序设计:Lab5

    Programming Create a Class Student which should have the following information:Parameters passed to ...

  2. jade成段的文本和标签

    如果文本是大段的文本怎么写呢?因为文本会换行,比如有多行的文本,标签,文本傻傻分不清楚 p 1234567890 => <p>1234567890</p> 如果需要换行, ...

  3. 【BZOJ2324】[ZJOI2011]营救皮卡丘 给定起点最小权K链可相交覆盖

    #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef ][]; int main() { i ...

  4. 【经典/基础BFS+略微复杂的题意】PAT-L3-004. 肿瘤诊断

    L3-004. 肿瘤诊断 在诊断肿瘤疾病时,计算肿瘤体积是很重要的一环.给定病灶扫描切片中标注出的疑似肿瘤区域,请你计算肿瘤的体积. 输入格式: 输入第一行给出4个正整数:M.N.L.T,其中M和N是 ...

  5. 22 webpack结合Vue使用的总结

    总结梳理:webpack中如何使用vue: 1.安装vue的包:cnpm i vue -S 2.由于 在webpack中,推荐使用 .vue这个组件模板文件定义组件, 所以,需要安装 能解析这种文件的 ...

  6. fsLayuiPlugin入门使用

    简介 源码下载后,不能直接打开,必须运行在容器下,例如:nginx.tomcat.jetty等容器. 源码中默认配置了nginx容器,可以直接启动nginx访问. 本文主要介绍下载源码后的使用,避免在 ...

  7. mysql双主模式方案

    MySQL双主(主主)架构方案   在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主从方案,一主多从,读写分离等,但是单主存在单点故障,从库切换成主库需要作改动.因此,如果 ...

  8. Windows 创建Raid

    Windows 常见raid有0.1和5,以下操作在虚拟机下模拟,学会这招在自己电脑做个raid也未尝不可啊~ 一.RAID 0 创建: 添加两块硬盘,联机并初始化(2T以下选MBR,以上选GPT) ...

  9. sql server 事务和锁的作用

    事务 事务就是作为一个逻辑工作单元的SQL语句,如果任何一个语句操作失败那么整个操作就被失败,以后操作就会回滚到操作前状态,或者是上个节点.为了确保要么执行,要么不执行,就可以使用事务.而锁是实现事务 ...

  10. The backup set holds a backup of a database other than the existing ‘dbName’ database

     [Solved] System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than t ...