IT虾米网

Android View转换成图片保存详解

flyfish 2018年06月22日 手机开发 607 0
import java.io.File;   
import java.io.FileOutputStream;   
   
import android.os.Bundle;   
import android.os.Environment;   
import android.os.Handler;   
import android.view.View;   
import android.widget.Button;   
import android.widget.TextView;   
import android.app.Activity;   
import android.graphics.Bitmap;   
import android.graphics.Canvas;   
import android.graphics.Color;   
import android.graphics.Paint;   
   
/**  
 * 把Android的一个View转换成图片保存  
 *   
 * */   
   
public class MainActivity extends Activity {   
   
    @Override   
    protected void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        setContentView(R.layout.activity_main);   
   
        final TextView tv = (TextView) findViewById(R.id.textView);   
        tv.setBackgroundColor(Color.GREEN);   
        // tv.setDrawingCacheEnabled(true);   
   
        final Runnable runnable = new Runnable() {   
            @Override   
            public void run() {   
                viewSaveToImage(tv);   
            }   
        };   
   
        Button button = (Button) findViewById(R.id.button);   
        button.setOnClickListener(new View.OnClickListener() {   
   
            @Override   
            public void onClick(View v) {   
                new Handler().post(runnable);   
            }   
        });   
   
    }   
   
    public void viewSaveToImage(View view) {   
        view.setDrawingCacheEnabled(true);   
        view.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);   
        view.setDrawingCacheBackgroundColor(Color.WHITE);   
   
        // 把一个View转换成图片   
        Bitmap cachebmp = loadBitmapFromView(view);   
   
        // 添加水印   
        Bitmap bitmap = Bitmap.createBitmap(createWatermarkBitmap(cachebmp,   
                "@ Zhang Phil"));   
   
        FileOutputStream fos;   
        try {   
            // 判断手机设备是否有SD卡   
            boolean isHasSDCard = Environment.getExternalStorageState().equals(   
                    android.os.Environment.MEDIA_MOUNTED);   
            if (isHasSDCard) {   
                // SD卡根目录   
                File sdRoot = Environment.getExternalStorageDirectory();   
                File file = new File(sdRoot, "test.PNG");   
                fos = new FileOutputStream(file);   
            } else   
                throw new Exception("创建文件失败!");   
   
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);   
   
            fos.flush();   
            fos.close();   
   
        } catch (Exception e) {   
            e.printStackTrace();   
        }   
   
        view.destroyDrawingCache();   
    }   
   
    private Bitmap loadBitmapFromView(View v) {   
        int w = v.getWidth();   
        int h = v.getHeight();   
   
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);   
        Canvas c = new Canvas(bmp);   
   
        c.drawColor(Color.WHITE);   
        /** 如果不设置canvas画布为白色,则生成透明 */   
   
        v.layout(0, 0, w, h);   
        v.draw(c);   
   
        return bmp;   
    }   
   
    // 为图片target添加水印   
    private Bitmap createWatermarkBitmap(Bitmap target, String str) {   
        int w = target.getWidth();   
        int h = target.getHeight();   
   
        Bitmap bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);   
        Canvas canvas = new Canvas(bmp);   
   
        Paint p = new Paint();   
   
        // 水印的颜色   
        p.setColor(Color.RED);   
   
        // 水印的字体大小   
        p.setTextSize(16);   
   
        p.setAntiAlias(true);// 去锯齿   
   
        canvas.drawBitmap(target, 0, 0, p);   
   
        // 在中间位置开始添加水印   
        canvas.drawText(str, w / 2, h / 2, p);   
   
        canvas.save(Canvas.ALL_SAVE_FLAG);   
        canvas.restore();   
   
        return bmp;   
    }   
}  

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

实现Android手机摄像头的自动对焦详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。