`
gryphone
  • 浏览: 427288 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

Bitmap 相关2:Bitmap.createBitmap() 使用

阅读更多
写道

 Bitmap 创建

 

写道
我们不能总是依赖于BitmapFactory 下面告诉大家怎么从Bitmaqp中截取某一部分创建新的Bitmap

系统会有一个默认png图片:icon.png 但是这个图片中最外层会有白色的 比较讨厌 现在以此为例 说说怎么截取 因为其外层为白色 显示不出来 所以我用了 *.9.png 作为其边界

 

 

 

[代码 步骤]

1. 创建Bitmaop 且指向icon.png

Bitmap ori = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);

 

 

2. 创建布局文件 有2个ImageView 一个供原图显示 一个供切割后显示

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout"
    >
<ImageView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/image1"
    android:layout_gravity="center_horizontal"
    />
<ImageView  
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/image2"
    android:layout_gravity="center"
    />
</LinearLayout>

 

 

3. 初始化变量

lLauout = (LinearLayout)findViewById(R.id.layout);
        
iv1 = (ImageView)findViewById(R.id.image1);
iv2 = (ImageView)findViewById(R.id.image2);

 

4. 得到原图的宽度与高度 供后面使用

width = ori.getWidth();
height = ori.getHeight();

 

5.  定义变量 标志切割位置 并初始化之

int startX,startY,lengthX,lengthY;

 

startX = 0;
startY = 0;

lengthX = width;
lengthY = height;

 

 

 6. 如何选取图片位置

写道
函数原型: Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height)


方法1:不断调整参数:x,y,width,heighy

方法2:利用导航键 上下左右分别控制上述4变量


导航键 左: x
导航键 右: width
导航键 上: y
导航键 下: height

使之向图片中央靠拢 且按下一下 移动固定的距离

 

public boolean onKeyDown(int keyCode, KeyEvent msg){
    	
    	switch(keyCode){
    	case KeyEvent.KEYCODE_DPAD_LEFT:
    		updateLeft();
    		break;
    		
    	case KeyEvent.KEYCODE_DPAD_RIGHT:
    		updateRight();
    		break;
    	case KeyEvent.KEYCODE_DPAD_UP:
    		updateUp();
    		break;
    	case KeyEvent.KEYCODE_DPAD_DOWN:
    		updateDown();
    		break;
    	case KeyEvent.KEYCODE_DPAD_CENTER:
    		showResult();
    		break;
    	}
    	
    	return false;
    }

 

 

7.  还需要判断移动是否合理

以下几种情况不合理:

1. 当图形左边 比 图形最大宽度 还大
2. 当图形上边 比 图形最大高度 还大
3. 图形宽度 或 高度 小于 0

 

public boolean isUpdateOK(){
    	if((startX > lengthX)||(startY > lengthY)||(lengthX > 0)||(lengthY > 0)){
    		return false;
    	}
    	else {
    		return true;
    	}
    }

 

 

8. 具体移动方法:

public void updateLeft(){
    	startX += step;
    	lengthX = width-startX;
        lengthY = height-startY;
        
        if(isUpdateOK()){
        	target1.recycle();
        	
        	target1 = Bitmap.createBitmap(ori,startX, startY, lengthX, lengthY);
        	
        	iv2.setImageBitmap(target1);
        	
        	setContentView(lLauout);
        }
    }
    public void updateUp(){
    	startY += step;
    	lengthX = width-startX;
        lengthY = height-startY;
        
        if(isUpdateOK()){
        	target1.recycle();
        	
        	target1 = Bitmap.createBitmap(ori,startX, startY, lengthX, lengthY);
        	
        	iv2.setImageBitmap(target1);
        	
        	setContentView(lLauout);
        }
    }
    public void updateRight(){
        lengthX -= step;
        
        if(isUpdateOK()){
        	target1.recycle();
        	
        	target1 = Bitmap.createBitmap(ori,startX, startY, lengthX, lengthY);
        	
        	iv2.setImageBitmap(target1);
        	
        	setContentView(lLauout);
        }
    }
    public void updateDown(){
        lengthY -= step;
        
        if(isUpdateOK()){
        	target1.recycle();
        	
        	target1 = Bitmap.createBitmap(ori,startX, startY, lengthX, lengthY);
        	
        	iv2.setImageBitmap(target1);
        	
        	setContentView(lLauout);
        }
    }
    
    
    public void showResult(){
    	AlertDialog.Builder ab = new AlertDialog.Builder(this);
    	AlertDialog aDialog;
    	
    	ab.setMessage("startX:"+startX+"\n"+"startY:"+startY+"\n"+"lengthX:"+lengthX+"\n"+"lengthY:"+lengthY).setTitle("show result").show();;
    	aDialog = ab.create();

    	aDialog.show();
    	
    }

 

 

 

9. emulator 运行情况: 

 

分享到:
评论

相关推荐

    TPNGImage 1.5

    Bitmap := TBitmap.Create; PNG := TPNGObject.Create; {In case something goes wrong, free booth Bitmap and PNG} try Bitmap.LoadFromFile(Source); PNG.Assign(Bitmap); //Convert data into png PNG....

    处理bitmap内存溢出问题

    处理bitmap内存溢出问题

    android bitmap outOfMemory解决方法

    android bitmap outofMemory 用来解决android中常见的bitmap outOfMemory

    Delphi 全屏抓图 范例.rar

     Fullscreen:=TBitmap.Create;//创建一个BITMAP来存放图象  Fullscreen.Width:=screen.width;  Fullscreen.Height:=screen.Height;  DC:=GetDC(0);//取得屏幕的DC,参数0指的是屏幕  FullscreenCanvas:=TCanvas...

    系统截屏源码

    //创建一个BITMAP来存放图象 Fullscreen.Width:=screen.width; Fullscreen.Height:=screen.Height; DC:=GetDC(0); //取得屏幕的DC,参数0指的是屏幕 FullscreenCanvas:=TCanvas.Create; //创建一个CANVAS对象 ...

    FairyGUI-Unity-Plugin-3.4.0.zip

    - IMPROVED: Change the way of registering bitmap font. - FIXED: A GButton pivot issue. - FIXED: Correct text align behavior. 2.3.0 - NEW: Allow loader to load component. - NEW: Add text template ...

    Javascript的bitmap处理库jsBitmap.zip

    一、Bitmap.create(width, height, bgcolor) 创建一个width x height像素大小的位图,底色为bgcolor所代表的颜色。 如:bitmap.create(10, 10, 0xff0000); // 创建一个10 x 10像素的底色为红色的位图 二、Bitmap.to...

    android Bitmap用法总结

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height/2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint ...

    Delphi以特效方式显示图片的示例.rar

     Rect1,Rect2:TRect;//源、目标矩形区域  Bitmap:=TBitmap.Create;  Bitmap.LoadFromFile&#40;'示例图片.bmp'&#41;;//装入位图文件  RatioX:=Bitmap.Width/Step;  for I:=0 to Step do  begin  MidX:=Round...

    CreateBitmap-Test-20200310.rar

    介绍CreateBitmap函数使用的三个例子。

    Zxing封装的二维码生成算法单元

    使用场景可用于各种文本信息转二维码图片中,如微信、支付宝主扫账单支付生成二维码等。 调用方法如下: procedure TMainSweep.LoadQrCode(AQRCode: string; Img: TImage); var QRCode: TDelphiZXingQRCode; Row, ...

    Delphi 检测程序是否运行,全屏抓图、修改计算机名称.rar

     Bitmap:=TBitmap.Create; //Bitmap为TBitmap类型  Bitmap.Handle:=BHandle;  Bitmap.SaveToFile&#40;'C:\My Documents\FullScreen.bmp'&#41; ;  DeleteDC(DestDC) ;  ReleaseDC(BHandle,SourceDC) ; //释放...

    自制MP3播放器 (TMediaPlayer/BmpShape)

    end else begin if Starty&lt;0 then StartY:=j else if j=(Picture.Bitmap.Height-1) then //最下面一个点 begin rgn2:=CreateRectRgn(i,StartY,i+1,j); CombineRgn(rgn1,rgn1,rgn2,RGN_OR); end; end; end else //不...

    获取桌面DLL程序.rar

    桌面截图DLL,函源码 ... ABM := TBitmap.Create; try ABM := getdeskbmp; image1.Picture.Bitmap := ABM; image1.Width:=ABM.Width; image1.Height:=ABM.Height; finally FreeAndNil(ABM); end; end;

    Delphi7 PngImage控件 PNGimage for Delph7

    使用方法:(Delphi 7 使用成功案例) 1、将本包中所有文件复制到开发文件保存的目录中 2、在USES单元中加入pngimage 3、窗体中插入image1控件 4、在调用位置,加入如下代码就OK啦! var png: tpngimage; begin ...

    Delphi 屏 幕 拷 贝 程 序

    //SCREEN对象是DELPHI预先定义的屏幕对象,直接使用就行了。 ---- 看 了 以 上 代 码, 你 就 会 发 现 用DELPHI 写 屏 幕 拷 贝 程 序 的 确 很 简 单。 ---- 当 然 要 写 一 个 实 用 的 屏 幕 拷 贝 程 序...

    PROGRAMMING ACTIONSCRIPT 3.0

    Chapter 2: Getting started with ActionScript..27 Programming fundamentals27 What computer programs do..27 Variables and constants...28 Data types...29 Working with objects.. 31 Properties...31 Methods...

    Delphi 7.0 彻底更换桌面壁纸及剪贴板监控程序范例.rar

    Delphi彻底更换桌面壁纸及剪贴板监控程序,需要使用到下列delphi类库:  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,  StdCtrls, registry, Clipbrd, ExtCtrls{监视剪贴板单元}; ...

    Gray-Image.zip_PByteArray_Refresh_ Refresh_procedure Grayscale

    raise exception.create(‘Error’) end for iHeight := 0 to Image.Picture.Bitmap.Height - 1 do begin pScanLine := Image.Picture.Bitmap.ScanLine[iHeight] iWidth := 0 while(iWidth &lt;= ...

    Android代码-auto-scroll-capture

    Bitmap bitmap = Bitmap.createBitmap(container.getWidth(), container.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); container.draw(canvas); &gt; 具体参考 DrawScrollViewAct ...

Global site tag (gtag.js) - Google Analytics