Android imitation photo, second shot, video cover selection. There are pictures with truth.

Source: Internet
Author: User

Android imitation photo, second shot, video cover selection. There are pictures with truth.

I am too lazy to add comments when there are not many codes. Forgive me for being lazy.

 

No thread is added for obtaining the thumbnail. FC may occur on some low-end servers or HD videos. You need to add the required children's shoes.

 

Code:

 

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


 

The code is messy because it is a demo. Let's take a look. Haha.

 

Demo link: An apk in the bin directory can be dragged directly to the simulator to see the effect.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.