我也有微信朋友圈了 Android实现

本文实例分享了一个简单的朋友圈程序,包含了朋友圈的列表实现,视频的录制、预览与上传,图片可选择拍照,供大家参考,具体内容如下

FriendsListActivity  代码如下

public class FriendsListActivity extends BaseActivity implements OnRefreshListener<ListView>, PostListener {
  private InteractionAdapter mAdapter;
  private PullToRefreshListView mRefreshListView;
  private View mFooter;
  private Context mContext;
  private Button btnToPost;
  protected int mPage = 0;
  private boolean isRefreshing = false;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.friends_list);
    mContext=getApplicationContext();
    mAdapter = new InteractionAdapter(mContext);
    mAdapter.setListener(this);
    btnToPost=(Button) findViewById(R.id.btn_topost);
    mRefreshListView = (PullToRefreshListView) findViewById(R.id.friends_list);
    FriendsApi.getFriendsList(mContext, mCallBack);
    mRefreshListView.setOnRefreshListener(this);
    mFooter = LayoutInflater.from(mContext).inflate(R.layout.loading_footer, null);
//   mRefreshListView.getRefreshableView().addFooterView(mFooter);
    mRefreshListView.setAdapter(mAdapter);
//   mRefreshListView.setOnLastItemVisibleListener(mLastListener);
//   mRefreshListView.getRefreshableView().setDividerHeight(40);
    btnToPost.setOnClickListener(new OnClickListener() { 

      @Override
      public void onClick(View v) {
        myPosts();
      }
    });
  } 

  protected void myPosts() {
    new AlertDialog.Builder(this).setItems(new String[]{"图片","视频","文字"}, new DialogInterface.OnClickListener() { 

      @Override
      public void onClick(DialogInterface dialog, int which) {
        Intent intent=new Intent();
        switch (which) {
        case 0:
          intent.setClass(FriendsListActivity.this, CreatePostActivity.class);
          break;
        case 1:
          intent.setClass(FriendsListActivity.this, RecorderActivity.class);
          break;
        case 2:
          intent.setClass(FriendsListActivity.this, RecorderActivity.class);
          break;
        default:
          break;
        }
        startActivity(intent);
      }
    }).show(); 

  }
  /**
   * 查看更多操作
   */
  @Override
  public void show(Interaction interaction) { 

  } 

  @Override
  public void delete(Interaction interaction) {
    // TODO Auto-generated method stub 

  } 

  @Override
  public void onRefresh(PullToRefreshBase<ListView> refreshView) {
    if (!isRefreshing) {
      isRefreshing = true;
      mPage = 0;
      FriendsApi.getFriendsList(mContext, mCallBack);
    }
  } 

  protected NetCallBack mCallBack = new NetCallBack() { 

    public void friendslist(ArrayList<Interaction> friends) {
      Log.i("friends size>>>>",friends.size()+"-------------");
      mAdapter.setInteractions(friends);
//     mRefreshListView.getLoadingLayoutProxy().setLastUpdatedLabel(null);
      mRefreshListView.onRefreshComplete();
      isRefreshing = false;
      dismissLoading();
    }; 

    public void start() {
      showLoading();
    }; 

    public void failed(String message) {
      loadFailed();
    };
  };
  @Override
  public void play(Interaction interaction) {
    Intent mIntent=new Intent();
    mIntent.setClass(FriendsListActivity.this, RecorderPlayActivity.class);
    Bundle data = new Bundle();
    data.putString("path", interaction.videoPath);
    mIntent.putExtras(data);
    startActivity(mIntent);
  }
}

布局文件 friends_list.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/backgroud_color" >
  <include
    android:id="@+id/list_title"
    android:layout_alignParentTop="true"
    layout="@layout/list_title"/> 

  <com.yzl.xyb.friends.refresh.view.PullToRefreshListView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/friends_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/padding_left"
    android:divider="@android:color/transparent"
    android:layout_below="@+id/list_title"
    app:ptrOverScroll="false"
    app:ptrHeaderTextColor="#ff666666"
    app:ptrHeaderTextAppearance="@android:style/TextAppearance.Small"
    app:ptrShowIndicator="false"/>
   <include layout="@layout/loading"/>
</RelativeLayout>

适配器 InteractionAdapter 对朋友圈列表进行数据填充

public class InteractionAdapter extends BaseAdapter implements OnClickListener {
  private ArrayList<Interaction> interactions;
  private Context mContext;
  private FinalBitmap mFinal;
  private BitmapDisplayConfig config;
  private BitmapDisplayConfig imageConfig;
  private PostListener listener;
  public InteractionAdapter(Context context) {
    mContext = context;
    mFinal = FinalBitmap.create(mContext);
    Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.user_avatar);
    config = new BitmapDisplayConfig();
    config.setAnimationType(BitmapDisplayConfig.AnimationType.fadeIn);
    config.setLoadfailBitmap(bitmap);
    config.setLoadfailBitmap(bitmap); 

    bitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.image_failed);
    imageConfig = new BitmapDisplayConfig();
    imageConfig.setAnimationType(BitmapDisplayConfig.AnimationType.fadeIn);
    imageConfig.setLoadfailBitmap(bitmap);
    imageConfig.setLoadfailBitmap(bitmap);
  } 

  public void setListener(PostListener listener) {
    this.listener = listener;
  } 

  public void setInteractions(ArrayList<Interaction> interactions) {
    this.interactions = interactions;
    notifyDataSetChanged();
  } 

  @Override
  public int getCount() {
    // TODO Auto-generated method stub
    return interactions == null ? 0 : interactions.size();
  } 

  @Override
  public Object getItem(int position) {
    // TODO Auto-generated method stub
    return interactions.get(position);
  } 

  @Override
  public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
  } 

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder holder = null;
    if (convertView == null) {
      convertView = LayoutInflater.from(mContext).inflate(R.layout.friend_list_item, null);
      holder = new ViewHolder();
      holder.avatar = (CircleImageView) convertView.findViewById(R.id.avatar);
      holder.content = (TextView) convertView.findViewById(R.id.content);
      holder.title = (TextView) convertView.findViewById(R.id.title);
      holder.subtitle = (TextView) convertView.findViewById(R.id.subtitle);
      holder.image = convertView.findViewById(R.id.image_layout);
      holder.image0 = (ImageView) convertView.findViewById(R.id.image0);
      holder.image1 = (ImageView) convertView.findViewById(R.id.image1);
      holder.image2 = (ImageView) convertView.findViewById(R.id.image2);
      holder.conments = (TextView) convertView.findViewById(R.id.conment_count);
      holder.praises = (TextView) convertView.findViewById(R.id.parise_count);
      holder.praised = (ImageView) convertView.findViewById(R.id.praise_icon);
      holder.more = (TextView) convertView.findViewById(R.id.more);
      holder.viewLayout=(LinearLayout) convertView.findViewById(R.id.view_layout);
      holder.surfaceView=(SurfaceView) convertView.findViewById(R.id.surface_view_result);
      holder.playButton=(ImageButton) convertView.findViewById(R.id.btn_play_result);
      holder.audioLayout=(FrameLayout) convertView.findViewById(R.id.audio_layout);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    } 

    Interaction interaction = interactions.get(position);
    if (TextUtils.isEmpty(interaction.avatar)) {
      holder.avatar.setImageBitmap(config.getLoadfailBitmap());
    } else {
      mFinal.display(holder.avatar, interaction.avatar, config);
    }
    holder.title.setText(interaction.name);
    holder.subtitle.setText(interaction.subtitle);
    holder.content.setText(interaction.content); 

    holder.conments.setText(String.valueOf(interaction.commentCount));
    holder.praises.setText(String.valueOf(interaction.praiseCount)); 

    int images = interaction.images == null ? 0 : interaction.images.size(); 

    if (images > 0) {
      holder.image.setVisibility(View.VISIBLE);
      holder.audioLayout.setVisibility(View.GONE);
      holder.image.setOnClickListener(this);
      holder.image.setTag(interaction);
      if (images <= 1) {
        mFinal.display(holder.image0, interaction.images.get(0), imageConfig);
        holder.image1.setImageBitmap(null);
        holder.image2.setImageBitmap(null);
      } else if (images <= 2) {
        mFinal.display(holder.image0, interaction.images.get(0), imageConfig);
        mFinal.display(holder.image1, interaction.images.get(1), imageConfig);
        holder.image2.setImageBitmap(null);
      } else {
        mFinal.display(holder.image0, interaction.images.get(0), imageConfig);
        mFinal.display(holder.image1, interaction.images.get(1), imageConfig);
        mFinal.display(holder.image2, interaction.images.get(2), imageConfig); 

      }
    } else if(interaction.videoPath!=null)
    {
        holder.image.setVisibility(View.GONE);
        holder.playButton.setBackgroundResource(R.drawable.play1pressed);
        holder.audioLayout.setVisibility(View.VISIBLE);
        holder.playButton.setTag(interaction);
        holder.playButton.setOnClickListener(this);
        holder.surfaceView.setTag(interaction);
        holder.surfaceView.setOnClickListener(this);
    }else{
      holder.viewLayout.setVisibility(View.GONE);
    } 

    holder.more.setTag(interaction);
    holder.more.setOnClickListener(this); 

    return convertView;
  } 

  private class ViewHolder {
    CircleImageView avatar;
    TextView title;
    TextView subtitle;
    TextView content;
    View image;
    ImageView image0;
    ImageView image1;
    ImageView image2;
    TextView conments;
    TextView praises;
    ImageView praised;
    View delete;
    TextView more;
    SurfaceView surfaceView;
    ImageButton playButton;
    FrameLayout audioLayout;
    LinearLayout viewLayout;
  } 

  @Override
  public void onClick(View v) {
    int id = v.getId();
    if (id == R.id.btn_play_result) {
      Interaction interaction = (Interaction) v.getTag();
    }else if (id == R.id.surface_view_result) {
      if (this.listener != null) {
        this.listener.play((Interaction) v.getTag());
      }
    }else if (id == R.id.more) {
      if (this.listener != null) {
        this.listener.show((Interaction) v.getTag());
      }
    } else if (id == R.id.image_layout) {
      Intent intent = new Intent(mContext, MainActivity.class);
      Bundle data = new Bundle();
      Interaction interaction = (Interaction) v.getTag();
      data.putStringArrayList("images", interaction.images);
      intent.putExtras(data);
      mContext.startActivity(intent);
    }
  } 

  public interface PostListener {
    void show(Interaction interaction);
    void delete(Interaction interaction);
    void play(Interaction interaction);
  }

多图片选择实现代码

MultipleActivity

package com.yzl.xyb.friends; 

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List; 

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.GridView;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast; 

import com.yzl.xyb.friends.adapter.MyAdapter;
import com.yzl.xyb.friends.adapter.MyAdapter.SetCountListener;
import com.yzl.xyb.friends.picture.ListImageDirPopupWindow;
import com.yzl.xyb.friends.picture.ListImageDirPopupWindow.OnImageDirSelected;
import com.yzl.xyb.friends.util.ImageFloder;
/**
 * 从相册选取图片
 * 可以选择多张,最多可选9张
 * 获取所有相册
 * 确定:返回已选图片的路径
 * @author hou
 *
 */
public class MultipleActivity extends Activity implements OnImageDirSelected, SetCountListener{
  private TextView selectCount;
  private TextView selectPicture;
  private TextView mChooseDir;
  private ProgressDialog mProgressDialog;
  public static final int KITKAT_LESS = 2;
  /**
   * 存储文件夹中的图片数量
   */
  private int mPicsSize;
  /**
   * 图片数量最多的文件夹
   */
  private File mImgDir;
  /**
   * 所有的图片
   */
  private List<String> mImgs;
  private ArrayList<String> pictures; 

  private GridView mGirdView;
  private MyAdapter mAdapter;
  /**
   * 临时的辅助类,用于防止同一个文件夹的多次扫描
   */
  private HashSet<String> mDirPaths = new HashSet<String>(); 

  /**
   * 扫描拿到所有的图片文件夹
   */
  private List<ImageFloder> mImageFloders = new ArrayList<ImageFloder>(); 

  private RelativeLayout mBottomLy; 

  int totalCount = 0; 

  private int mScreenHeight; 

  private ListImageDirPopupWindow mListImageDirPopupWindow; 

  private Handler mHandler = new Handler()
  {
    public void handleMessage(android.os.Message msg)
    {
      mProgressDialog.dismiss();
      // 为View绑定数据
      data2View();
      // 初始化展示文件夹的popupWindw
      initListDirPopupWindw();
    }
  }; 

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.picture_selector);
    getIntent().getExtras();
    DisplayMetrics outMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(outMetrics);
    mScreenHeight = outMetrics.heightPixels; 

    initView();
    getImages();
    initEvent(); 

  } 

  /**
   * 初始化View
   */
  private void initView()
  {
    mGirdView = (GridView) findViewById(R.id.id_gridView);
    mChooseDir = (TextView) findViewById(R.id.id_choose_dir);
    selectCount = (TextView) findViewById(R.id.tv_select_count);
//   allPhotoAlum = (TextView) findViewById(R.id.tv_photoAlum);
    selectPicture= (TextView) findViewById(R.id.tv_sure);
    mBottomLy = (RelativeLayout) findViewById(R.id.id_bottom_ly);
  } 

  private void initEvent()
  {
    /**
     * 为底部的布局设置点击事件,弹出popupWindow
     */
    mBottomLy.setOnClickListener(new OnClickListener()
    {
      @Override
      public void onClick(View v)
      {
        mListImageDirPopupWindow
            .setAnimationStyle(R.style.anim_popup_dir);
        mListImageDirPopupWindow.showAsDropDown(mBottomLy, 0, 0); 

        // 设置背景颜色变暗
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = .3f;
        getWindow().setAttributes(lp);
      }
    }); 

    selectPicture.setOnClickListener(new OnClickListener() { 

      @Override
      public void onClick(View v) {
        pictures=mAdapter.getSelectPath();
        Log.i("选中的图片1>>>>>>",pictures.size()+"----------");
        Intent intent=new Intent();
//       intent.setClass(MultipleActivity.this, CreatePostActivity.class);
        Bundle bundle=new Bundle();
        bundle.putStringArrayList("PICTURES", pictures);
        intent.putExtras(bundle);
//       startActivityForResult(intent, KITKAT_LESS);
        setResult(KITKAT_LESS, intent);
        finish();
      }
    });
  } 

  /**
   * 为View绑定数据
   */
  private void data2View()
  {
    if (mImgDir == null)
    {
      Toast.makeText(getApplicationContext(), "擦,一张图片没扫描到",
          Toast.LENGTH_SHORT).show();
      return;
    } 

    mImgs = Arrays.asList(mImgDir.list());
    /**
     * 可以看到文件夹的路径和图片的路径分开保存,极大的减少了内存的消耗;
     */
    mAdapter = new MyAdapter(getApplicationContext(), mImgs,
        R.layout.grid_item, mImgDir.getAbsolutePath());
    mAdapter.setCountListener(this);
    mGirdView.setAdapter(mAdapter);
//   allPictureCount.setText("共"+totalCount + "张");
  }; 

  /**
   * 初始化展示文件夹的popupWindw
   */
  private void initListDirPopupWindw()
  {
    mListImageDirPopupWindow = new ListImageDirPopupWindow(
        LayoutParams.MATCH_PARENT, (int) (mScreenHeight * 1),
        mImageFloders, LayoutInflater.from(getApplicationContext())
            .inflate(R.layout.list_dir, null)); 

    mListImageDirPopupWindow.setOnDismissListener(new OnDismissListener()
    { 

      @Override
      public void onDismiss()
      {
        // 设置背景颜色变暗
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = 1.0f;
        getWindow().setAttributes(lp);
      }
    });
    // 设置选择文件夹的回调
    mListImageDirPopupWindow.setOnImageDirSelected(this);
  } 

  /**
   * 利用ContentProvider扫描手机中的图片,此方法在运行在子线程中 完成图片的扫描,最终获得jpg最多的那个文件夹
   */
  private void getImages()
  {
    if (!Environment.getExternalStorageState().equals(
        Environment.MEDIA_MOUNTED))
    {
      Toast.makeText(this, "暂无外部存储", Toast.LENGTH_SHORT).show();
      return;
    }
    // 显示进度条
    mProgressDialog = ProgressDialog.show(this, null, "正在加载..."); 

    new Thread(new Runnable()
    {
      @Override
      public void run()
      { 

        String firstImage = null; 

        Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver mContentResolver = MultipleActivity.this
            .getContentResolver(); 

        // 只查询jpeg和png的图片
        Cursor mCursor = mContentResolver.query(mImageUri, null,
            MediaStore.Images.Media.MIME_TYPE + "=? or "
                + MediaStore.Images.Media.MIME_TYPE + "=?",
            new String[] { "image/jpeg", "image/png" },
            MediaStore.Images.Media.DATE_MODIFIED); 

        Log.e("TAG", mCursor.getCount() + "");
        while (mCursor.moveToNext())
        {
          // 获取图片的路径
          String path = mCursor.getString(mCursor
              .getColumnIndex(MediaStore.Images.Media.DATA)); 

          Log.e("TAG", path);
          // 拿到第一张图片的路径
          if (firstImage == null)
            firstImage = path;
          // 获取该图片的父路径名
          File parentFile = new File(path).getParentFile();
          if (parentFile == null)
            continue;
          String dirPath = parentFile.getAbsolutePath();
          ImageFloder imageFloder = null;
          // 利用一个HashSet防止多次扫描同一个文件夹(不加这个判断,图片多起来还是相当恐怖的~~)
          if (mDirPaths.contains(dirPath))
          {
            continue;
          } else
          {
            mDirPaths.add(dirPath);
            // 初始化imageFloder
            imageFloder = new ImageFloder();
            imageFloder.setDir(dirPath);
            imageFloder.setFirstImagePath(path);
          } 

          int picSize = parentFile.list(new FilenameFilter()
          {
            @Override
            public boolean accept(File dir, String filename)
            {
              if (filename.endsWith(".jpg")
                  || filename.endsWith(".png")
                  || filename.endsWith(".jpeg"))
                return true;
              return false;
            }
          }).length;
          totalCount += picSize; 

          imageFloder.setCount(picSize);
          mImageFloders.add(imageFloder); 

          if (picSize > mPicsSize)
          {
            mPicsSize = picSize;
            mImgDir = parentFile;
          }
        }
        mCursor.close(); 

        // 扫描完成,辅助的HashSet也就可以释放内存了
        mDirPaths = null; 

        // 通知Handler扫描图片完成
        mHandler.sendEmptyMessage(0x110); 

      }
    }).start(); 

  } 

  @Override
  public void selected(ImageFloder floder)
  { 

    mImgDir = new File(floder.getDir());
    mImgs = Arrays.asList(mImgDir.list(new FilenameFilter()
    {
      @Override
      public boolean accept(File dir, String filename)
      {
        if (filename.endsWith(".jpg") || filename.endsWith(".png")
            || filename.endsWith(".jpeg"))
          return true;
        return false;
      }
    }));
    /**
     * 可以看到文件夹的路径和图片的路径分开保存,极大的减少了内存的消耗;
     */
    mAdapter = new MyAdapter(getApplicationContext(), mImgs,
        R.layout.grid_item, mImgDir.getAbsolutePath());
    mAdapter.setCountListener(this);
    mGirdView.setAdapter(mAdapter);
//    mAdapter.notifyDataSetChanged();
//   mImageCount.setText(floder.getCount() + "张");
    mChooseDir.setText(floder.getName());
    selectCount.setText("/9");
    mListImageDirPopupWindow.dismiss(); 

  } 

  @Override
  public void doCount(int a) {
    selectCount.setText(a+"/9");
  } 

}

视频的录制与预览

package com.yzl.xyb.friends; 

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.Toast; 

import com.yzl.xyb.friends.view.MovieRecorderView;
import com.yzl.xyb.friends.view.MovieRecorderView.OnRecordFinishListener;
/**
 * 录制视频
 * @author hou
 *
 */
public class RecorderActivity extends Activity { 

  private MovieRecorderView mRecorderView;
  private Button mShootBtn;
  private boolean isFinish = true;
  private String userId = "";
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.record_activity);
//   userId=getIntent().getParcelableExtra("userId");
    mRecorderView = (MovieRecorderView) findViewById(R.id.movieRecorderView);
    mShootBtn = (Button) findViewById(R.id.shoot_button); 

    mShootBtn.setOnTouchListener(new OnTouchListener() { 

      @Override
      public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
          mRecorderView.record(new OnRecordFinishListener() { 

            @Override
            public void onRecordFinish() {
              Log.i("MotionEvent>>>","ACTION_DOWN");
              handler.sendEmptyMessage(1);
            }
          });
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
          Log.i("MotionEvent>>>","ACTION_UP");
          if (mRecorderView.getTimeCount() > 1)
            handler.sendEmptyMessage(1);
          else {
            if (mRecorderView.getmVecordFile() != null)
              mRecorderView.getmVecordFile().delete();
            mRecorderView.stop();
            Toast.makeText(RecorderActivity.this, "时间太短,录制失败", Toast.LENGTH_SHORT).show();
          }
        }
        return true;
      }
    });
  } 

  @Override
  public void onResume() {
    super.onResume();
    isFinish = true;
  } 

  @Override
  public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    isFinish = false;
    mRecorderView.stop();
  } 

  @Override
  public void onPause() {
    super.onPause();
  } 

  @Override
  public void onDestroy() {
    super.onDestroy();
  } 

  @SuppressLint("HandlerLeak")
  private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      finishActivity();
      Log.i("isFinish>>>",isFinish+"");
    }
  }; 

  private void finishActivity() {
    if (isFinish) {
      mRecorderView.stop(); 

      Intent intent = new Intent(RecorderActivity.this, TopicActivity.class);
      Bundle mBundle = new Bundle();
      mBundle.putString("path", mRecorderView.getmVecordFile().toString());
      mBundle.putString("userId", userId);
      intent.putExtras(mBundle);
      startActivity(intent);
    }
  }  

  public interface OnShootCompletionListener {
    public void OnShootSuccess(String path, int second);
    public void OnShootFailure();
  }
}

视频的预览

package com.yzl.xyb.friends; 

import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView; 

public class RecorderPlayActivity extends Activity implements SurfaceHolder.Callback, OnClickListener { 

  private ImageView ivBack;
  private ImageButton btnPlay;
  private SurfaceView surfaceView;
  private SurfaceHolder surfaceHolder;
  private String path=null;
  private MediaPlayer player;
  private boolean play=false; 

  @SuppressWarnings("deprecation")
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.recorder_play);
    ivBack=(ImageView) findViewById(R.id.iv_back);
    btnPlay=(ImageButton) findViewById(R.id.ib_play);
    surfaceView=(SurfaceView) findViewById(R.id.play_view);
    btnPlay.setBackground(getResources().getDrawable(R.drawable.play1pressed));
    path=this.getIntent().getStringExtra("path");
    System.out.println("surface created>>>> path= "+path);
    surfaceHolder=surfaceView.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setFixedSize(320, 220);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    System.out.println("oncreate--------------");
    ivBack.setOnClickListener(this);
    btnPlay.setOnClickListener(this);
    surfaceView.setOnClickListener(this);
  } 

  @Override
  public void surfaceCreated(SurfaceHolder holder) {
    player=new MediaPlayer();
    player.setAudioStreamType(AudioManager.STREAM_MUSIC);
    player.setDisplay(surfaceHolder);
    try {
      System.out.println("surface created>>>> path= "+path);
      player.setDataSource(path);
      player.prepare();
    } catch (Exception e) {
      e.printStackTrace();
    }
  } 

  @Override
  public void surfaceChanged(SurfaceHolder holder, int format, int width,
      int height) {
    // TODO Auto-generated method stub 

  } 

  @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
    // TODO Auto-generated method stub 

  } 

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.iv_back:
      this.finish();
      break;
    case R.id.ib_play:
      player.start();
      btnPlay.setVisibility(View.GONE);
      break;
    case R.id.play_view:
      player.pause();
      /*if(play){
        player.start();
      }else {
        player.pause();
      }*/
      btnPlay.setVisibility(View.VISIBLE);
      break; 

    default:
      break;
    }
  } 

  @Override
  protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if(player.isPlaying())
    {
      player.stop();
    }
    player.release();
  }
}

拥有一个属于自己的朋友圈是不是很开新,可以和好朋友随时随地分享,是不是很开心!

以上就是本文的全部内容,希望对大家学习Android软件编程有所帮助。

(0)

相关推荐

  • Android实现微信朋友圈发本地视频功能

    一.前言 前一篇文章已经详细介绍了如何使用Xposed框架编写第一个微信插件:摇骰子和猜拳作弊器 本文继续来介绍如何使用Xposed框架编写第二个微信插件,可以将本地小视频发布到朋友圈的功能.在这之前我们还是要有老套路,准备工作要做好,这里还是使用微信6.3.9版本进行操作,准备工作: 1.使用apktool工具进行反编译,微信没有做加固防护,所以这个版本的微信包反编译是没有任何问题的. 2.借助于可视化反编译工具Jadx打开微信包,后续几乎重要分析都是借助这个工具来操作的. 二.猜想与假设 做

  • 基于js实现微信发送好友如何分享到朋友圈、微博

    微信浏览器内置了javascript私有对象WeixinJSBridge,可以实现发送给朋友.分享到朋友圈.分享到微博等功能. <script> var imgUrl = "图片地址"; var lineLink = "当前网址"; var descContent = "描述"; var shareTitle = '标题'; var appid = ''; function shareFriend() { WeixinJSBridge

  • java开发微信分享到朋友圈功能

    微信分享功能开发 用了一天时间,把微信发送给朋友和分享到朋友圈功能开发出来,在这里给大家分享一下,避免大家走弯路. 一.服务器端程序 package com.wiimedia.controller; import java.io.IOException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.text.ParseException; import

  • Android仿微信发朋友圈浏览图片效果

    先看一下效果吧: 下面就来说一下具体怎么实现的: 实现思路 1.首先我们要获取数据源,数据源就是我们的每条说说(包括姓名.标题.图片数组) 2.自定义适配器(ListView嵌套着GridView) 3.图片点击浏览图片(Fragment+ViewPager) 具体实现 1.初始化数据源,设置适配器,看一下代码: public class MyActivity extends Activity { /*图片显示列表*/ private ListView listView; /*图片URL数组*/

  • Android GridView仿微信朋友圈显示图片

    最近项目要求上传多图并且多图显示,而且要规则的显示,就像微信朋友圈的图片显示一样. 利用GridView再适合不过了,GridView可以动态加载图片的数量,而且还比较规律,下面说一下自己的思路: 1.获取网络图片 2.初始化gridview,自定义适配器 3.根据图片数量设置gridview的列数 4.更新适配器 下面贴上部分源码并给大家解析一下 一.首先是GridView的item <com.view.SquareLayout xmlns:android="http://schemas

  • 我也有微信朋友圈了 Android实现

    本文实例分享了一个简单的朋友圈程序,包含了朋友圈的列表实现,视频的录制.预览与上传,图片可选择拍照,供大家参考,具体内容如下 FriendsListActivity  代码如下 public class FriendsListActivity extends BaseActivity implements OnRefreshListener<ListView>, PostListener { private InteractionAdapter mAdapter; private PullTo

  • Android仿微信朋友圈全文、收起功能的实例代码

    前言 一般在社交APP中都有类似朋友圈的功能,其中发表的动态内容很长的时候不可能让它全部显示.这里就需要做一个仿微信朋友圈全文.收起功能来解决该问题.在网上看到一个例子-->http://www.jb51.net/article/105251.htm,写的很不错,但是有个bug,他这个Demo只有在条目固定的时候才正常,当增加.删除条目的时候会出现全文.收起显示混乱的问题.原因是他使用了固定的position作为key来保存当前显示的状态.这篇文章在他的基础上进行优化. 效果图 具体代码 (详细

  • Android 实现微信,微博,微信朋友圈,QQ分享的功能

    Android 实现微信,微博,微信朋友圈,QQ分享的功能 一.去各自所在的开发者平台注册相应的Key值:引入相关jar包.权限等 二.ShareUtil工具类 import android.app.Activity; import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.graphics.Bit

  • Android 高仿微信朋友圈拍照上传功能

    模仿微信朋友圈发布动态,输入文字支持文字多少高度自增,有一个最小输入框高度,输入文字有限制,不过这些都很easy! 1. PhotoPicker的使用 这是一个支持选择多张图片,点击图片放大,图片之间左右滑动互相切换的库,同时支持图片删除的库,效果类似微信. (1) 添加PhotoPicker的架包 (2) 使用 选择图片:安卓6.0以后需要在代码中添加读写sd卡和相机的权限 当然清单文件中也需要添加的 PhotoPicker.builder() .setPhotoCount(maxPhoto)

  • Android 仿微信朋友圈点赞和评论弹出框功能

    贡献/下载源码:https://github.com/mmlovesyy/PopupWindowDemo 本文简单模仿微信朋友圈的点赞和评论弹出框,布局等细节请忽略,着重实现弹出框.发评论,及弹出位置的控制. 1. 微信弹出框 微信朋友圈的点赞和评论功能,有2个组成部分: 点击左下角的"更多"按钮,弹出对话框: 点击评论,弹出输入框,添加评论并在页面中实时显示: 微信朋友圈点赞和评论功能 2. 实际效果 本文将建一个 ListView,在其 Item 中简单模仿微信的布局,然后着重实现

  • Android仿微信朋友圈实现滚动条下拉反弹效果

    微信朋友圈上面的图片封面,QQ空间说说上面的图片封面都有下拉反弹的效果,这些都是使用滚动条实现的.下拉,当松开时候,反弹至原来的位置.下拉时候能看到背景图片.那么这里简单介绍一下这种效果的实现. 1.效果图 这部手机显示的分辨率有限,很老的手机调试. 2.具有反弹效果BounceScrollView package com.org.scroll; import android.content.Context; import android.graphics.Rect; import androi

  • 关于Android实现简单的微信朋友圈分享功能

    1.先下载微信分享的jar包放在lib目录下,并且添加依赖, 清单文件添加 <activity android:name=".wxapi.WXEntryActivity" android:exported="true">//产生回调 </activity> 2.在工程下新建如图所示的包和Activity Activity里面的内容主要是创建微信api并且注册返回回调值 private final String APP_ID= "wx

  • Android 高仿微信朋友圈动态支持双击手势放大并滑动查看图片效果

    最近参与了开发一款旅行APP,其中包含实时聊天和动态评论功能,终于耗时几个月几个伙伴完成了,今天就小结一下至于实时聊天功能如果用户不多的情况可以scoket实现,如果用户万级就可以采用开源的smack + opnefile实现,也可以用mina开源+XMMP,至于怎么搭建和实现,估计目前github上一搜一大把,至于即时通讯怕误人子弟,暂且不做介绍,现就把实现的一个微信朋友圈的小功能介绍一下. 先上效果图: 一拿到主流的UI需求,大致分析下,需要我ListView嵌套Gridview,而grid

  • Android自定义TextView仿微信朋友圈文字展开全文功能

    Android自定义TextView仿微信朋友圈文字信息,展开全文功能 代码及注释如下: 首先写一个xml文件 showmore.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical

随机推荐