怎么写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location) (一)

如何写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location) (一)

先上效果图:

怎么写一个发微博的页面(包括插入图片,插入表情,插入话题,插入Location) (一)


先看页面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" 
	android:layout_height="fill_parent"
	android:orientation="vertical"
	android:background="@android:color/white" >
	
	<RelativeLayout
		android:layout_width="fill_parent"
		android:layout_height="0dp"
		android:layout_weight="1"
		android:background="#f4f4f4" >
		
		<LinearLayout
			android:id="@+id/btns_bottom"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:layout_alignParentBottom="true"
			android:orientation="horizontal">
			
			
			<LinearLayout <strong> //这5个是底部的按钮</strong>
				android:layout_width="fill_parent"
				android:layout_height="wrap_content"
				android:layout_weight="1">
				<ImageButton
					android:src="@drawable/btn_insert_save"
					android:layout_width="fill_parent"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:background="@drawable/bg_btn"
					android:visibility="gone" />
				
				<ImageButton
					android:id="@+id/ib_insert_location"
					android:src="@drawable/btn_insert_location"
					android:layout_width="fill_parent"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:background="@drawable/bg_btn"/>
					
				<ImageButton
					android:id="@+id/ib_insert_pic"
					android:src="@drawable/btn_insert_pic"
					android:layout_width="fill_parent"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:background="@drawable/bg_btn"/>
				
				<ImageButton
					android:id="@+id/ib_insert_topic"
					android:src="@drawable/btn_insert_talk"
					android:layout_width="fill_parent"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:background="@drawable/bg_btn"/>
				
				<ImageButton
					android:id="@+id/ib_insert_at"
					android:src="@drawable/btn_insert_at"
					android:layout_width="fill_parent"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:background="@drawable/bg_btn"/>
				
				<ImageButton
					android:id="@+id/ib_face_keyboard"
					android:src="@drawable/btn_insert_face"
					android:layout_width="fill_parent"
					android:layout_height="wrap_content"
					android:layout_weight="1"
					android:background="@drawable/bg_btn"/>
			</LinearLayout>
		</LinearLayout>
	
		<RelativeLayout <strong> //这一个包括缩略图和location</strong>
			android:id="@+id/marks"
			android:layout_width="fill_parent"
			android:layout_height="50dp"
			android:padding="3dp"
			android:layout_above="@id/btns_bottom" >
			
			<ImageView
				android:id="@+id/iv_insertpic"
				android:scaleType="centerCrop"
				android:layout_width="50dp"
				android:layout_height="fill_parent"
				android:visibility="gone" />
			
			<ImageView
				android:id="@+id/iv_location"
				android:src="@drawable/icon_insertlocation"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:layout_centerVertical="true"
				android:layout_marginLeft="15dp"
				android:layout_toRightOf="@id/iv_insertpic"
				android:visibility="gone"/>
				
			<ProgressBar
				android:id="@+id/pg_loadlocation"
				style="@android:style/Widget.ProgressBar.Small.Inverse"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:layout_centerVertical="true"
				android:layout_marginLeft="15dp"
				android:layout_toRightOf="@id/iv_insertpic"
				android:visibility="gone" />
			
			<LinearLayout
				android:id="@+id/ll_text_limit_unit"
				android:layout_width="wrap_content"
				android:layout_height="wrap_content"
				android:layout_centerVertical="true"
				android:layout_alignParentRight="true"
				android:gravity="center"
				android:background="@drawable/bg_delwords"
				android:focusable="true"
				android:clickable="true" android:layout_alignParentBottom="false">
				
				<TextView
					android:id="@+id/tv_text_limit"
					android:layout_width="wrap_content"
					android:layout_height="wrap_content"
					android:text="140"
					android:layout_marginRight="5px"
					android:textColor="#333"/>
				
				<ImageView
					android:src="@drawable/icon_delwords"
					android:layout_width="wrap_content"
					android:layout_height="wrap_content" />
				
			</LinearLayout>
			
		</RelativeLayout>
		
		<EditText<strong> //这个是正文</strong>
			android:id="@+id/et_mblog"
			android:layout_width="fill_parent"
			android:layout_height="fill_parent"
			android:layout_above="@id/marks"
			android:background="@null"
			android:capitalize="sentences"
			android:imeOptions="actionDone"
			android:gravity="top|left"
			android:layout_margin="3px" />
	
	</RelativeLayout>
	<strong>//这个在最外层,整个界面的底部,是表情的界面,用gridview来实现</strong>
	<com.example.weibotest.view.EmotionView
		android:id="@+id/emotion_view"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:visibility="gone" />
</LinearLayout>


首先看从手机相册里load image:

private void startToMediaActivity() {
		startActivityForResult(new Intent(Intent.ACTION_PICK,
				android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),
				WriteBlog.REQUEST_CODE_GALLERY);
	}

private void startToCameraActivity() {
		if (FileUtils.isSdCardAvailable()) {
			final Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
			final Uri picUri = Uri.fromFile(sdcardTempFile);
			i.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
			startActivityForResult(i, WriteBlog.REQUEST_CODE_CAMERA);
		}
		else {
			Toast.makeText(this, R.string.pls_insert_sdcard, Toast.LENGTH_SHORT)
					.show();
		}
	}
	

一个Image的处理类:

	final class ImageLoadingHelper {
		
		private File picfile;
		private WriteBlog activity;
		private String defaultTempPath;
		private String defaultDraftPath;
		
		Bitmap createThumbnail() {
			Options thumbnailOpts = new BitmapFactory.Options();
			if (!hasBitmap()) { throw new IllegalStateException("There is no pic!"); }
			return BitmapFactory.decodeFile(getTempPath(),
					thumbnailOpts);
		}
		
		synchronized boolean hasBitmap() {
			String picfileName = picfile.getName();
			if(picfileName.matches(".avi")|| picfileName.matches(".rm")||picfileName.matches(".mp4")
					||picfileName.matches(".rmvb")||picfileName.matches(".wmv")||picfileName.matches(".flv"))
				{
				return false;
				}
			else{
				return FileUtils.doesExisted(picfile);
			}
			
		}
		
		synchronized void importBitmapFile(Uri uri) {
			if (uri.getScheme().equals("content")) {
				InputStream inputStream = null;
				try {
					inputStream = activity.getContentResolver()
							.openInputStream(uri);
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				FileUtils.file_put_contents(inputStream,picfile);
			}
		}
		
		
		boolean doesDraftExist() {
			return FileUtils.doesExisted(defaultDraftPath);
		}
		

		String getTempPath() {
			return picfile == null ? "" : picfile.getAbsolutePath();
		}
		
		
		void loadDraft() {
			FileUtils.copy(defaultDraftPath, defaultTempPath);
			activity.displayInsertBitmap();
			
		}
		
		void saveDraft() {
			FileUtils.copy(defaultTempPath, defaultDraftPath);
		}
		
		
		ImageLoadingHelper(WriteBlog activity) {
			this.activity = activity;
			if (FileUtils.isSdCardAvailable()) {
				defaultTempPath = FileUtils.getSDPath() + "/temp/"
						+ System.currentTimeMillis() + ".jpg";
			}
			else {
				defaultTempPath = activity.getFilesDir().getAbsolutePath()
						+ "/pic/" + System.currentTimeMillis() + ".jpg";
			}
			defaultDraftPath = activity.getFilesDir().getAbsolutePath()
					+ "/draft/bitmap_temp.jpg";
			if (FileUtils.doesExisted(defaultTempPath)) {
				defaultTempPath.replace(".jpg", "(1).jpg");
			}
			picfile = new File(defaultTempPath);
		}
		
	}



选完图片我们会调用onactivityResult:

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if ((requestCode == WriteBlog.REQUEST_CODE_CAMERA)
				|| (WriteBlog.REQUEST_CODE_GALLERY == requestCode)) {
			if (resultCode != Activity.RESULT_OK) { return; }
			Uri uri = null;
			if ((requestCode == WriteBlog.REQUEST_CODE_CAMERA)
					&& FileUtils.doesExisted(sdcardTempFile)) {
				uri = Uri.fromFile(sdcardTempFile);
			}
			else if (WriteBlog.REQUEST_CODE_GALLERY == requestCode) {
				uri = data.getData();
			}

			if (uri != null) {
				displayImageUri(uri);
			}
		}
	}
	
	private void displayImageUri(Uri uri) {
		mImageLoadingHelper.importBitmapFile(uri);
		displayInsertBitmap();
	}
	
	void displayInsertBitmap() {
		insertPicView.setImageBitmap(mImageLoadingHelper
				.createThumbnail());
		insertPicView.setVisibility(View.VISIBLE);
	}