Android bitmap的拷贝以及平移旋转缩放等效果

package com.example.bitmap;


import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;


public class MainActivity extends Activity {
private ImageView iv1;
private ImageView iv2;
private Bitmap srcBitmap;
private Bitmap alterBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv1=(ImageView) findViewById(R.id.iv1);
iv2=(ImageView) findViewById(R.id.iv2);
//1.给v1设置一个位图
srcBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
iv1.setImageBitmap(srcBitmap);

//2.创建一个空白的位图
alterBitmap=Bitmap.createBitmap(srcBitmap.getWidth()*3, srcBitmap.getHeight(), srcBitmap.getConfig());
}

public void click(View view){
//1.把空白的位图给了画板  准备画画
Canvas canvas=new Canvas(alterBitmap);
//2.设置画笔
Paint paint=new Paint();
paint.setAntiAlias(true);  //消除锯齿
//3.画位图

Matrix m=new Matrix();
//1.设置缩放的比例
//m.setScale(1.0f, 2.0f);
//2.指定圆心的旋转90度
//m.setRotate(90,srcBitmap.getWidth()/2,srcBitmap.getHeight()/2);
//3.镜子效果
/*m.setScale(-1.0f, 1.0f);//先镜子到y轴的左侧
m.postTranslate(srcBitmap.getWidth(), 0);//然后在向右平移到屏幕上
        */
//4.倒影效果
/*m.setScale(1.0f, -1.0f);//线倒影要图片的下面  
m.postTranslate(0, srcBitmap.getHeight());//然后向上平移到原来的位置*/
//5.平移效果  
   m.setTranslate(100,20);
canvas.drawBitmap(srcBitmap,m, paint);
iv2.setImageBitmap(alterBitmap);
}
}
发布了55 篇原创文章 · 获赞 5 · 访问量 6万+
展开阅读全文

android保存当前view变成bitmap,并保存bitmap到本地

09-28

做了一个保存当前view成bitmap图片的项目,但最终报如下错误,找了半天也找不到解决之法。 --------------------------------- public static Bitmap getBitmapByView(ScrollView scrollView, HorizontalScrollView hsv) { int w = 0; int h = 0; Bitmap bitmap = null; // 获取scrollview实际高度 for (int i = 0; i < scrollView.getChildCount(); i++) { h += scrollView.getChildAt(i).getHeight(); scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff")); } // 获取HorizontalScrollView实际宽度 for (int i = 0; i < hsv.getChildCount(); i++) { w += hsv.getChildAt(i).getWidth(); hsv.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff")); } // 创建对应大小的bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565); final Canvas canvas = new Canvas(bitmap); scrollView.draw(canvas); hsv.draw(canvas); return bitmap; } /** * 压缩图片 * * @param image * @return */ public static Bitmap compressImage(Bitmap image) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG, 100, baos); // 把压缩后的数据baos存放到ByteArrayInputStream中 ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); // 把ByteArrayInputStream数据生成图片 Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null); return bitmap; } /** * 保存到sdcard * * @param b * @return */ public static String savePic(Bitmap b) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US); File outfile = new File("/sdcard/image"); // 如果文件不存在,则创建一个新文件 if (!outfile.isDirectory()) { try { outfile.mkdir(); } catch (Exception e) { e.printStackTrace(); } } String fname = outfile + "/" + sdf.format(new Date()) + ".png"; FileOutputStream fos = null; try { fos = new FileOutputStream(fname); if (null != fos) { b.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.flush(); fos.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return fname; } --------------------------------------------- 09-28 14:31:11.181 23514-23514/com.bonc.ms.main I/ViewRootImpl: finishMotionEvent: handled = true stage=10: View Post IME stage,inputElapseTime=0 eventTime = 867860380 downTime = 867860380 title= com.bonc.ms.main/com.bonc.ms.main.develop.FourGDevelopActivity 09-28 14:31:11.287 23514-23514/com.bonc.ms.main V/SettingsInterface: invalidate [system]: current 4765 != cached 0 09-28 14:31:11.289 23514-23514/com.bonc.ms.main D/ActivityThread: holder:android.app.IActivityManager$ContentProviderHolder@30ba67e, holder.provider:android.content.ContentProviderProxy@c2477df 09-28 14:31:11.398 23514-23520/com.bonc.ms.main W/art: Suspending all threads took: 58.163ms 09-28 14:31:12.773 23514-23724/com.bonc.ms.main D/skia: jpeg_decoder mode 1, colorType 4, w 7907, h 4586, sample 1, bsLength 0!! 09-28 14:31:13.227 23514-23724/com.bonc.ms.main D/skia: jpeg_decoder finish successfully, L:1934!!! 09-28 14:31:13.229 23514-23724/com.bonc.ms.main W/System.err: java.io.FileNotFoundException: /sdcard/image/2016-09-28_14-31-13.png: open failed: ENOENT (No such file or directory) 09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at libcore.io.IoBridge.open(IoBridge.java:487) 09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87) 09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:127) 09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:116) 09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at com.bonc.ms.tools.ScreenShot.savePic(ScreenShot.java:110) 09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at com.bonc.ms.main.develop.FourGDevelopActivity$3.run(FourGDevelopActivity.java:153) 09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: at java.lang.Thread.run(Thread.java:818) 09-28 14:31:13.231 23514-23724/com.bonc.ms.main W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) 09-28 14:31:13.232 23514-23724/com.bonc.ms.main W/System.err: at libcore.io.Posix.open(Native Method) 09-28 14:31:13.232 23514-23724/com.bonc.ms.main W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 09-28 14:31:13.232 23514-23724/com.bonc.ms.main W/System.err: at libcore.io.IoBridge.open(IoBridge.java:473) 09-28 14:31:13.232 23514-23724/com.bonc.ms.main W/System.err: ... 6 more 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览