Android:EditText插入图片实现图文混排

package com.mw.guahu.activity.createentry;


import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentResolver;
import android.content.DialogInterface;
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.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.mw.guahu.R;
import com.mw.guahu.activity.BaseActivity;


/*
 * Create entry
 */
@SuppressLint("NewApi")
public class CreateEntry extends BaseActivity implements OnClickListener {
private ImageView mcreate_entry_a;// entry@
private ImageView mcreate_entry_picture;// Entry pictures
private TextView mdialog_cancel;// cancel
private TextView mentry_release;// release
private final int PICK_PIC = 1; 
private EditText et_addimage;
private int mImgViewWidth;
private float mInsertedImgWidth;
private ViewTreeObserver vto;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
vto = et_addimage.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
   @Override
   public void onGlobalLayout() {
       et_addimage.getViewTreeObserver().removeOnGlobalLayoutListener(this);
       mImgViewWidth = et_addimage.getWidth();
       mInsertedImgWidth = mImgViewWidth * 0.8f;
   }
}); 
}


private void initView() {

mcreate_entry_a = (ImageView) this.findViewById(R.id.create_entry_a);
mcreate_entry_picture = (ImageView) this.findViewById(R.id.create_entry_picture);
mdialog_cancel = (TextView) this.findViewById(R.id.dialog_cancel);
mentry_release = (TextView) this.findViewById(R.id.entry_release);
et_addimage=(EditText) this.findViewById(R.id.edittext);
mcreate_entry_a.setOnClickListener(this);
mcreate_entry_picture.setOnClickListener(this);
mdialog_cancel.setOnClickListener(this);
mentry_release.setOnClickListener(this);


}


@Override
protected void initInnerLayout() {
setSubContentView(R.layout.activity_create_entry);
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.create_entry_a:


break;
case R.id.create_entry_picture:
Intent intent = new Intent(Intent.ACTION_PICK);
   intent.setType("image/*");
   startActivityForResult(intent, PICK_PIC);
break;


case R.id.dialog_cancel:// Cancel
finish();
break;
case R.id.entry_release:// release
Toast.makeText(CreateEntry.this, "正在开发中...", Toast.LENGTH_SHORT).show();
break;


default:
break;
}
}

@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);
           }
       }
   }


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());
}


/**
* 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 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;

}



activity_create_entry.xml



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?color_more_background" >


    <RelativeLayout
        android:id="@+id/titls"
        android:layout_width="match_parent"
        android:layout_height="58dp"
        android:background="?color_main_bottom_textView" >


        <TextView
            android:id="@+id/dialog_cancel"
            android:layout_width="53dp"
            android:layout_height="23dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="17dp"
            android:layout_marginRight="27dp"
            android:background="@drawable/classify_seacher"
            android:clickable="true"
            android:gravity="center"
            android:text="@string/dialog_cancel"
            android:textColor="?color_main_bottom_textView"
            android:textSize="12sp" />


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="@string/entry_new"
            android:textColor="?color_white"
            android:textSize="17sp" />


        <TextView
            android:id="@+id/entry_release"
            android:layout_width="53dp"
            android:layout_height="23dp"
            android:layout_alignBaseline="@+id/dialog_cancel"
            android:layout_alignBottom="@+id/dialog_cancel"
            android:layout_alignParentRight="true"
            android:layout_marginRight="17dp"
            android:background="@drawable/classify_seacher"
            android:clickable="true"
            android:gravity="center"
            android:text="@string/entry_release"
            android:textColor="?color_main_bottom_textView"
            android:textSize="12sp" />
    </RelativeLayout>


    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="match_parent"
        android:layout_height="367dp"
        android:layout_below="@id/titls"
        android:layout_marginLeft="17dp"
        android:layout_marginRight="17dp"
        android:layout_marginTop="17dp"
        android:background="@drawable/classify_seacher"
        android:scrollbars="vertical" >


        <EditText
            android:id="@+id/edittext"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@null"
            android:gravity="top"
            android:hint="发布词条内容" />
    </ScrollView>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@null" >


        <ImageView
            android:id="@+id/create_entry_a"
            android:layout_width="23dp"
            android:layout_height="23dp"
            android:layout_alignTop="@+id/create_entry_picture"
            android:layout_marginRight="15dp"
            android:layout_toLeftOf="@+id/create_entry_picture"
            android:clickable="true"
            android:contentDescription="@null"
            android:src="@drawable/create_entry_a" />


        <ImageView
            android:id="@+id/create_entry_picture"
            android:layout_width="23dp"
            android:layout_height="23dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="108dp"
            android:layout_marginRight="24dp"
            android:clickable="true"
            android:contentDescription="@null"
            android:src="@drawable/create_entry_picture" />
    </RelativeLayout>


</RelativeLayout>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值