赞
踩
第一步:
申请权限
- <!-- 往SDCard写入数据权限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
第二步:
布局文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context=".MainActivity">
-
-
- <TextView
- android:layout_width="match_parent"
- android:layout_height="56dp"
- android:background="@color/mainColor"
- android:gravity="center"
- android:text="自动详情图"
- android:textColor="#fff"
- android:textSize="20sp" />
-
- <EditText
- android:id="@+id/etContent"
- android:layout_width="match_parent"
- android:layout_height="56dp"
- android:hint="输入文字"
- android:textColorHint="#C1C1C1" />
-
- <Button
- android:layout_width="match_parent"
- android:layout_height="56dp"
- android:onClick="Button"
- android:text="Button" />
-
- <RelativeLayout
- android:id="@+id/rlContent"
- android:layout_width="match_parent"
- android:layout_height="70dp"
- android:layout_marginTop="10dp"
- android:background="#D73ADC"
- android:gravity="center">
-
- <TextView
- android:id="@+id/tvContent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="文字"
- android:textColor="#fff"
- android:textSize="18sp" />
- </RelativeLayout>
-
- <ImageView
- android:id="@+id/ivPreview"
- android:layout_width="match_parent"
- android:layout_height="70dp"
- android:layout_marginTop="10dp"
- android:background="#C0C0C0"
- android:gravity="center" />
-
- </LinearLayout>
第三步:
Java代码
- package com.xiong.autodetail.activity;
-
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.os.Bundle;
- import android.os.Handler;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
-
- import com.squareup.picasso.Picasso;
- import com.xiong.autodetail.R;
- import com.xiong.autodetail.utils.FileUtil;
- import com.xiong.autodetail.utils.ToastUtil;
-
- import java.io.File;
- import java.io.FileOutputStream;
-
- public class MainActivity extends BaseActivity {
-
- private EditText etContent;
- private TextView tvContent;
- private RelativeLayout rlContent;
- private ImageView ivPreview;
- private Context context = this;
- final private static int REFRESH_COMPLETE = 0;//Message标记
- final private static int SET_TEXT_TIME = 1;//设置文本延时时间(单位毫秒),避免图片上文字生成不完整
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- initView();
-
- }
-
- //初始化控件
- private void initView() {
- etContent = (EditText) findViewById(R.id.etContent);
- tvContent = (TextView) findViewById(R.id.tvContent);
- rlContent = (RelativeLayout) findViewById(R.id.rlContent);
- ivPreview = (ImageView)findViewById(R.id.ivPreview);
- }
-
- //按钮响应事件
- public void Button(View view) {
- if(etContent.length() > 0){
- String content = etContent.getText().toString();
- tvContent.setText(content);
- mHandler.sendEmptyMessageDelayed(REFRESH_COMPLETE, SET_TEXT_TIME);
- }else{
- ToastUtil.show("未输入!");
- }
- }
-
- //Hadle
- private Handler mHandler = new Handler() {
- public void handleMessage(android.os.Message msg) {
- switch (msg.what) {
- case REFRESH_COMPLETE:
- save(getBitmap(rlContent));
- etContent.setText("");
- break;
- }
- }
- };
-
- //将View转为Bitmap
- public Bitmap getBitmap(View view) {
- view.setDrawingCacheEnabled(true);
- Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
- view.setDrawingCacheEnabled(false);
- return bitmap;
- }
-
- //将Bitmap保存为图片
- private void save(Bitmap bitmap) {
- try {
- String fileName = tvContent.getText().toString().substring(0, 2) + ".png";
- FileOutputStream stream = new FileOutputStream(FileUtil.getDownPath() + fileName);
- bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
- stream.flush();
- stream.close();
- ToastUtil.show("操作完成");
- previewImg(FileUtil.getDownPath() + fileName);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- //预览图片
- private void previewImg(String filePath){
- Picasso.with(context).load(new File(filePath)).into(ivPreview);
- }
-
- }
- public static String getDownPath() {
- String path = Environment.getExternalStorageDirectory() + "/000_AAA/";
- isDownFolder(path);
- return path;
- }
-
- //判断文件夹是否存在,如果不存在则创建文件夹
- public static void isDownFolder(String downFolder) {
- File file = new File(downFolder);
- if (!file.exists()) {
- file.mkdir();
- }
- }
效果图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。