图文混排

package com.example.picture;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.util.Log;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private final int PICK_PIC = 1; 
private EditText et_addimage;
private Button bu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_addimage=(EditText) this.findViewById(R.id.edittext);
}
public void add_img(View view){
Intent intent = new Intent(Intent.ACTION_PICK);
   intent.setType("image/*");
   startActivityForResult(intent, PICK_PIC);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   if (resultCode == RESULT_OK) {
       if (requestCode == PICK_PIC) {
           if (data == null) {
               Toast.makeText(this, "failed", Toast.LENGTH_SHORT).show();
           } else {
               Uri uri = data.getData();
               Bitmap bitmap = getOriginalBitmap(uri);
               SpannableString ss = getBitmapMime(bitmap, uri);
               insertIntoEditText(ss);
           }
       }
   }

/**
* EditText中可以接收的图片(要转化为SpannableString)

* @param pic
* @param uri
* @return SpannableString
*/
private SpannableString getBitmapMime(Bitmap pic, Uri uri) {
   int imgWidth = pic.getWidth();
   int imgHeight = pic.getHeight();
   // 只对大尺寸图片进行下面的压缩,小尺寸图片使用原图
//    if (imgWidth >= mInsertedImgWidth) {
//        float scale = (float) mInsertedImgWidth / imgWidth;
//        Matrix mx = new Matrix();
//        mx.setScale(scale, scale);
//        pic = Bitmap.createBitmap(pic, 0, 0, imgWidth, imgHeight, mx, true);
//    }
   String smile = uri.getPath();
   SpannableString ss = new SpannableString(smile);
   ImageSpan span = new ImageSpan(this, pic);
   ss.setSpan(span, 0, smile.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
   return ss;
}
private void insertIntoEditText(SpannableString ss) {
   // 先获取Edittext中原有的内容
   Editable et = et_addimage.getText();
   int start = et_addimage.getSelectionStart();
   // 设置ss要添加的位置
   et.insert(start, ss);
   // 把et添加到Edittext中
   et_addimage.setText(et);
   // 设置Edittext光标在最后显示
   et_addimage.setSelection(start + ss.length());
}
private Bitmap getOriginalBitmap(Uri photoUri) {
   if (photoUri == null) {
       return null;
   }
   Bitmap bitmap = null;
   try {
       ContentResolver conReslv = getContentResolver();
       // 得到选择图片的Bitmap对象
       bitmap = MediaStore.Images.Media.getBitmap(conReslv, photoUri);
   } catch (Exception e) {
   }
   return bitmap;

}

<RelativeLayout 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" >
    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:scrollbars="vertical">
        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="sfe"
            android:gravity="top"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@null" />
</ScrollView>
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:onClick="add_img"
            android:text="Button" />
    
</RelativeLayout>

import android.view.MenuItem;

你可能感兴趣的:(图文混排)