Android 仿美拍,秒拍 ,视频封面选择.有图有真相.

技术分享

代码不多,就懒得加注释了.原谅我的懒.


缩略图获取那块,没有加用线程.可能会在有些低端机上面或者高清的视频会出现FC的情况.需要的童鞋.自行添加.


附代码:

package com.tan.videoselectthumbnail;


import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.tan.videoselectthumbnail.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

	private static LinearLayout linearLayout;
	private static Context context;
	private RelativeLayout rl_l;
	private ImageView imageviewPreview,ivw_to2;
	private GestureDetector mGestureDetector;
	private RelativeLayout.LayoutParams lp;
	private List<Bitmap> bitmaps;
	private int wWidth,iWidth,tWidth;
	private int arrayWidth[];
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		linearLayout=(LinearLayout) findViewById(R.id.ll);
		context=this;
		rl_l=(RelativeLayout) findViewById(R.id.rl_l);
		imageviewPreview=(ImageView) findViewById(R.id.ivw_to);
		ivw_to2=(ImageView) findViewById(R.id.ivw_to2);
		Uri uri=Uri.parse("android.resource://" + getPackageName() + "/" +R.raw.test);  
		bitmaps=new ArrayList<Bitmap>();
		getVideoThumbnail(uri);
		rl_l.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				return (mGestureDetector != null)&& mGestureDetector.onTouchEvent(event);
			}
		});
		lp=(android.widget.RelativeLayout.LayoutParams) imageviewPreview.getLayoutParams();
		mGestureDetector = new GestureDetector(this,new CoverGestureListener());
		wWidth=getWindowManager().getDefaultDisplay().getWidth();
	}
	public void onWindowFocusChanged(boolean hasFocus) {
		iWidth=imageviewPreview.getWidth();
		iWidth=wWidth-iWidth;
		tWidth=iWidth/10;
		arrayWidth=new int[10];
		for(int i=0;i<10;i++){
			arrayWidth[i]=tWidth+(i*tWidth);
		}
		seekTo(0);
		super.onWindowFocusChanged(hasFocus);
	}
	private void seekTo(float paramFloat) {
		float f = paramFloat - this.imageviewPreview.getWidth() / 2;
		if(f<0){
			f=0;
		}if(f>iWidth){
			f=iWidth;
		}
		lp.leftMargin = ((int) f);
		for(int i=0;i<arrayWidth.length;i++){
			if(arrayWidth[i]>=f){
				imageviewPreview.setImageBitmap(bitmaps.get(i));
				ivw_to2.setImageBitmap(bitmaps.get(i));
				break;
			}
		}
		this.imageviewPreview.setLayoutParams(this.lp);
	}
	private  class CoverGestureListener extends GestureDetector.SimpleOnGestureListener {
		public boolean onDown(MotionEvent e) {
			seekTo(e.getX());
			return true;
		}
		public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {
			float f = e2.getX();
			seekTo(f);
			return super.onScroll(e1, e2, distanceX, distanceY);
		}
	}
	public void getVideoThumbnail(Uri uri) {
		Bitmap bitmap=null;
		MediaMetadataRetriever retriever = new MediaMetadataRetriever();
		try {
			retriever.setDataSource(context, uri);
			String time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
			int seconds=Integer.valueOf(time)/1000;
			int timeS=Integer.valueOf(time)/10;
			for(int i=1;i<=10;i++){
				bitmap=retriever.getFrameAtTime(i*timeS*1000,MediaMetadataRetriever.OPTION_CLOSEST);
				bitmaps.add(bitmap);
				addImgView(bitmap);
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			try {
				retriever.release();
			} catch (RuntimeException e) {
				e.printStackTrace();
			}
		}
	}
	public  void addImgView(Bitmap bitmap){
		SquareImageView imageView=new SquareImageView(context);
		LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		lp.weight=1;
		imageView.setLayoutParams(lp);
		imageView.setImageBitmap(bitmap);
		linearLayout.addView(imageView);
	}
}


因为是demo所以代码写的比较乱,将就着看吧.哈哈.


demo链接:bin目录下有apk 可直接拖到模拟器里面看效果.



链接:http://share.weiyun.com/62b725b7fde04130f671b7023c7e1aed (密码:M13t)



郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。