欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-16 19:00:28
 模仿微信朋友圈发布动态,输入文字支持文字多少高度自增,有一个最小输入框高度,输入文字有限制,不过这些都很easy! 1. photopicker的使用 这是...

 模仿微信朋友圈发布动态,输入文字支持文字多少高度自增,有一个最小输入框高度,输入文字有限制,不过这些都很easy!

1. photopicker的使用

这是一个支持选择多张图片,点击图片放大,图片之间左右滑动互相切换的库,同时支持图片删除的库,效果类似微信。

(1) 添加photopicker的架包

(2) 使用

    选择图片:安卓6.0以后需要在代码中添加读写sd卡和相机的权限 当然清单文件中也需要添加的

 photopicker.builder()
        .setphotocount(maxphoto) //设置最多选择的图片数量
        .setshowcamera(true)  //是否显示相机按钮
        .setselected(photos)  //设置已经选好的图片
        .start(senddynamicactivity.this); //上下文参数 需要传activity

    查看图片:

photopreview.builder()
        .setphotos(photos) //设置选择好的图片
        .setcurrentitem(position) //设置当前选择的条目
        .setshowdeletebutton(true) //是否显示删除按钮
        .start(this);

2. 发布朋友圈页面

代码比较简单直接上

package com.zlc.friendcirclephoto.ui;
import android.manifest;
import android.content.intent;
import android.os.bundle;
import android.support.v7.widget.recyclerview;
import android.support.v7.widget.staggeredgridlayoutmanager;
import android.support.v7.widget.helper.itemtouchhelper;
import android.text.inputfilter;
import android.view.view;
import android.widget.imageview;
import android.widget.textview;
import com.mylhyl.acp.acp;
import com.mylhyl.acp.acplistener;
import com.mylhyl.acp.acpoptions;
import com.zlc.friendcirclephoto.adapter.myitemtouchhelpercallback;
import com.zlc.friendcirclephoto.adapter.sendimageadapter;
import com.zlc.friendcirclephoto.view.commonedittext;
import com.zlc.friendcirclephoto.r;
import com.zlc.friendcirclephoto.utils.logutil;
import java.util.arraylist;
import java.util.collections;
import java.util.list;
import me.iwf.photopicker.photopicker;
import me.iwf.photopicker.photopreview;
/**
 * created by zlc
 * 发布朋友圈
 */
public class senddynamicactivity extends baseactivity implements view.onclicklistener,sendimageadapter.onclicklistener{
  private imageview im_fanhui;
  private textview tv_title;
  private commonedittext id_et_content;
  private textview id_tv_num;
  private textview id_tv_max_num;
  private sendimageadapter mphotoadapter;
  public static final int maxphoto = 9; //最大选择几张照片
  private list<object> images = new arraylist<>();
  private arraylist<string> photos = new arraylist<>();
  private recyclerview mrecycleview;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_send_note);
    initview();
    initdata();
    initlistener();
  }
  private void initview() {
    im_fanhui = findview(r.id.id_iv_back);
    tv_title = findview(r.id.id_tv_title);
    tv_title.settext("发布");
    id_et_content = findview(r.id.id_common_et);
    mrecycleview = findview(r.id.id_recycleview);
    id_tv_num = findview(r.id.tv_num);
    id_tv_max_num = findview(r.id.id_tv_max_num);
    id_et_content.sethint("这一刻的想法......");
    id_tv_num.settext("0");
    id_tv_max_num.settext("/188");
    id_et_content.setfilters(new inputfilter[]{new inputfilter.lengthfilter(188)});
  }
  private void initdata() {
    images.add(1);
    mrecycleview.setlayoutmanager(new staggeredgridlayoutmanager(3,staggeredgridlayoutmanager.vertical));
    mphotoadapter = new sendimageadapter(senddynamicactivity.this, images);
    mrecycleview.setadapter(mphotoadapter);
    mphotoadapter.setonclicklistener(this);
    myitemtouchhelpercallback callback = new myitemtouchhelpercallback(mphotoadapter);
    //实现拖拽排序
    final itemtouchhelper touchhelper = new itemtouchhelper(callback);
    //调用itemtouchhelper的attachtorecyclerview方法建立联系
    touchhelper.attachtorecyclerview(mrecycleview);
  }
  private void initlistener() {
    im_fanhui.setonclicklistener(this);
    id_et_content.setontextchaged(new commonedittext.ontextchaged() {
      @override
      public void settext(string s) {
        id_tv_num.settext(s.length()+"");
      }
    });
  }
  @override
  public void onclick(view v) {
    switch (v.getid()){
      case r.id.id_iv_back:
        finish();
        break;
    }
  }
  @override
  protected void onactivityresult(int requestcode, int resultcode, intent data) {
    super.onactivityresult(requestcode, resultcode, data);
    if (resultcode == result_ok && (requestcode == photopicker.request_code || requestcode == photopreview.request_code)) {
      list<string> photos = null;
      if (data != null) {
        photos = data.getstringarraylistextra(photopicker.key_selected_photos);
      }
      if (photos != null) {
        clearphotos(photos.size());
        images.addall(images.size()-1,photos);
        mphotoadapter.notifydatasetchanged();
      }
    }
  }
  @override
  public void onclick(view v, int position) {
    switch (v.getid()){
      case r.id.iv_add:
        selectphoto();
        break;
      case r.id.im_delete:
        delimages(position);
        break;
      case r.id.iv_img:
        lookphoto(position);
        break;
    }
  }
  public void delimages(int position){
    images.remove(position);
    if(mphotoadapter!=null)
      mphotoadapter.notifyitemremoved(position);
  }
  public void clearphotos(int size){
    logutil.e("返回有几张照片",size+"");
    images.clear();
    images.add(1);
  }
  //选择图片
  private void selectphoto() {
    list<object> images = mphotoadapter.getdatas();
    photos.clear();
    for (int i = 0;i< images.size()-1;i++ ){
      photos.add(images.get(i).tostring());
    }
    //权限
    acp.getinstance(this).request(new acpoptions.builder()
            .setpermissions(
                manifest.permission.write_external_storage,
                manifest.permission.camera,
                manifest.permission.read_external_storage
            )
            .build(),
        new acplistener() {
          @override
          public void ongranted() {
            logutil.e("msg", "权限外全部通过");
            photopicker.builder()
                .setphotocount(maxphoto)
                .setshowcamera(true)
                .setselected(photos)
                .start(senddynamicactivity.this);
          }
          @override
          public void ondenied(list<string> permissions) {
            logutil.e(permissions.tostring() ,"权限拒绝");
          }
        });
  }
  //查看图片
  private void lookphoto(int position){
    photos.clear();
    list<object> images = mphotoadapter.getdatas();
    for (int i = 0;i< images.size()-1;i++ ){
      photos.add(images.get(i).tostring());
    }
    photopreview.builder()
        .setphotos(photos)
        .setcurrentitem(position)
        .setshowdeletebutton(true) //是否显示删除按钮
        .start(this);
  }
}

3. 图片适配器代码

public class sendimageadapter extends recyclerview.adapter<sendimageadapter.viewholder> implements itemtouchhelperadapter{
  private activity mcontext;
  private list<object> mdatas;
  private layoutinflater mlayoutinflater;
  public sendimageadapter(activity context,list<object> datas){
    this.mcontext = context;
    this.mdatas = datas;
    mlayoutinflater = layoutinflater.from(context);
  }
  @override
  public sendimageadapter.viewholder oncreateviewholder(viewgroup parent, int viewtype) {
    view view = mlayoutinflater.inflate(r.layout.send_grid_item,parent,false);
    return new viewholder(view);
  }
  @override
  public void onbindviewholder(final sendimageadapter.viewholder viewholder, final int position) {
    object image = mdatas.get(position);
    linearlayout.layoutparams params = (linearlayout.layoutparams) viewholder.id_rl_img.getlayoutparams();
    int w = params.width = (int) ((screenutil.getscreenwidth(mcontext) - densityutil.dp2px(mcontext,38)) / 3.0);
    params.height = w;
    params.bottommargin = densityutil.dp2px(mcontext,7);
    params.rightmargin = densityutil.dp2px(mcontext,7);
    viewholder.id_rl_img.setlayoutparams(params);
    if (image instanceof string) {
      viewholder.im_delete.setvisibility(view.visible);
      viewholder.iv_add.setvisibility(view.gone);
      viewholder.im_img.setvisibility(view.visible);
      //这里最好使用glide picasso加载本地图片 bitmap容易造成内存溢出
      bitmap bitmap = bitmapfactory.decodefile(image.tostring());
      viewholder.im_img.setimagebitmap(bitmap);
      logutil.e("手机照片", image.tostring());
    } else {
      viewholder.im_delete.setvisibility(view.invisible);
      viewholder.iv_add.setvisibility(view.visible);
      viewholder.im_img.setvisibility(view.gone);
      viewholder.iv_add.setimageresource(r.drawable.tupiantianjia2x);
      logutil.e("添加照片", ""+image);
    }
    viewholder.iv_add.setonclicklistener(new myclicklistener(viewholder));
    viewholder.im_img.setonclicklistener(new myclicklistener(viewholder));
    viewholder.im_delete.setonclicklistener(new myclicklistener(viewholder));
  }
  @override
  public int getitemcount() {
    if(mdatas == null){
      return 0;
    }
    return mdatas.size() >= 9 ? 9 : mdatas.size();
  }
  public list<object> getdatas() {
    return mdatas;
  }
  public static class viewholder extends recyclerview.viewholder{
    private final imageview im_img;
    private final imageview iv_add;
    private final imageview im_delete;
    private final framelayout id_rl_img;
    public viewholder(view itemview) {
      super(itemview);
      im_img = (imageview) itemview.findviewbyid(r.id.iv_img);
      iv_add = (imageview) itemview.findviewbyid(r.id.iv_add);
      im_delete = (imageview) itemview.findviewbyid(r.id.im_delete);
      id_rl_img = (framelayout) itemview.findviewbyid(r.id.id_rl_img);
    }
  }
  //拖拽排序相关
  @override
  public void onitemmove(int frompos, int topos) {
    if(frompos == mdatas.size()-1 || topos == mdatas.size()-1 )
      return;
    collections.swap(mdatas,frompos,topos);
    notifyitemmoved(frompos,topos);
  }
  //滑动删除相关
  @override
  public void onitemdel(int pos) {
    if(pos == mdatas.size() - 1)
      return;
    mdatas.remove(pos);
    notifyitemremoved(pos);
  }
  public interface onclicklistener{
    void onclick(view v, int position);
  }
  private static onclicklistener onclicklistener;
  public void setonclicklistener(onclicklistener onclicklistener) {
    this.onclicklistener = onclicklistener;
  }
  public static class myclicklistener implements view.onclicklistener{
    private viewholder mholder;
    public myclicklistener(viewholder holder ){
      this.mholder = holder;
    }
    @override
    public void onclick(view view) {
      switch (view.getid()){
        case r.id.iv_add:
        case r.id.im_delete:
        case r.id.iv_img:
          if(onclicklistener!=null) {
            int pos = mholder.getadapterposition();
            onclicklistener.onclick(view, pos);
          }
          break;
      }
    }
  }
}

4. 拖拽排序

(1) recycleview通过itemtouchhelper 实现

 myitemtouchhelpercallback callback = new  myitemtouchhelpercallback(mphotoadapter);
  //实现拖拽排序
  final itemtouchhelper touchhelper = new itemtouchhelper(callback);
  //调用itemtouchhelper的attachtorecyclerview方法建立联系
  touchhelper.attachtorecyclerview(mrecycleview);

(2) myitemtouchhelpercallback的自定义

package com.zlc.friendcirclephoto.adapter;
import android.graphics.canvas;
import android.support.v7.widget.gridlayoutmanager;
import android.support.v7.widget.linearlayoutmanager;
import android.support.v7.widget.recyclerview;
import android.support.v7.widget.staggeredgridlayoutmanager;
import android.support.v7.widget.helper.itemtouchhelper;
public class myitemtouchhelpercallback extends itemtouchhelper.callback {
  private sendimageadapter madapter; //适配器传过来
  private boolean islongdrag = true;
  private boolean ismoveswipe = true;
  public myitemtouchhelpercallback(sendimageadapter adapter){
    this.madapter = adapter;
  }
  @override
  public int getmovementflags(recyclerview recyclerview, recyclerview.viewholder viewholder) {
    recyclerview.layoutmanager layoutmanager = recyclerview.getlayoutmanager();
    int dragflags = 0; //拖拽的标志
    int swipeflags = 0; //删除的标志
    int position = viewholder.getadapterposition();
    if(position == madapter.getdatas().size() - 1){ // 如果是最后一个条目 则既不能拖拽也不能删除
      dragflags = 0;
      swipeflags = 0;
    } else if(layoutmanager instanceof gridlayoutmanager || layoutmanager instanceof staggeredgridlayoutmanager){//网格布局 则支持上下左右拖拽 不支持删除
      dragflags = itemtouchhelper.up | itemtouchhelper.down | itemtouchhelper.left | itemtouchhelper.right; //允许上下左右的拖动
      swipeflags = 0; //不能删除
    }else if(layoutmanager instanceof linearlayoutmanager){ //线性布局分为垂直和水平
      linearlayoutmanager manager = (linearlayoutmanager) layoutmanager;
      int orientation = manager.getorientation();
      if(orientation == linearlayoutmanager.horizontal){ //横向listview列表 
        dragflags = itemtouchhelper.left | itemtouchhelper.right; //允许左右的拖动
        swipeflags = itemtouchhelper.down; //只允许从上向下侧滑
      }else if(orientation == linearlayoutmanager.vertical){ //竖向listview列表
        dragflags = itemtouchhelper.up | itemtouchhelper.down; //允许上下的拖动
        swipeflags = itemtouchhelper.left; //只允许从右向左侧滑删除
      }
    }else{  //最后一个条目既不能拖拽也不能删除 相当于更多
      dragflags = 0;
      swipeflags = 0;
    }
    return makemovementflags(dragflags,swipeflags);
  }
  @override
  public boolean onmove(recyclerview recyclerview, recyclerview.viewholder viewholder, recyclerview.viewholder target) {
    madapter.onitemmove(viewholder.getadapterposition(),target.getadapterposition());
    return true;
  }
  @override
  public void onswiped(recyclerview.viewholder viewholder, int direction) {
    madapter.onitemdel(viewholder.getadapterposition());
  }
  // 该方法返回true时,表示支持长按拖动
  @override
  public boolean islongpressdragenabled() {
    return islongdrag;
  }
  //该方法返回true时,表示支持滑动删除
  @override
  public boolean isitemviewswipeenabled() {
    return ismoveswipe;
  }
  //从静止状态变为拖拽或者滑动的时候会回调该方法,参数actionstate表示当前的状态。 开始拖拽的时候给item添加一个背景色,然后在拖拽完成的时候还原
  @override
  public void onselectedchanged(recyclerview.viewholder viewholder, int actionstate) {
    //拖拽的时候才设置背景颜色
//    if (actionstate != itemtouchhelper.action_state_idle && actionstate != itemtouchhelper.action_state_swipe) {
//      viewholder.itemview.setbackgroundcolor(color.ltgray);
//    }
    super.onselectedchanged(viewholder, actionstate);
  }
  //当用户操作完毕某个item并且其动画也结束后会调用该方法
  @override
  public void clearview(recyclerview recyclerview, recyclerview.viewholder viewholder) {
    //重置改变,防止由于复用而导致的显示问题
    super.clearview(recyclerview, viewholder);
    viewholder.itemview.setbackgroundcolor(0);
  }
  //我们可以在这个方法内实现我们自定义的交互规则或者自定义的动画效果
  @override
  public void onchilddraw(canvas c, recyclerview recyclerview, recyclerview.viewholder viewholder, float dx, float dy, int actionstate, boolean iscurrentlyactive) {
    super.onchilddraw(c, recyclerview, viewholder, dx, dy, actionstate, iscurrentlyactive);
  }
  public void setlongdrag(boolean longdrag) {
    islongdrag = longdrag;
  }
  public void setmoveswipe(boolean moveswipe) {
    ismoveswipe = moveswipe;
  }
}

(3) 具体实现方法

 //拖拽排序相关
  @override
  public void onitemmove(int frompos, int topos) {
    if(frompos == mdatas.size()-1 || topos == mdatas.size()-1 )
      return;
    collections.swap(mdatas,frompos,topos);
    notifyitemmoved(frompos,topos);
  }
  //滑动删除相关
  @override
  public void onitemdel(int pos) {
    if(pos == mdatas.size() - 1)
      return;
    mdatas.remove(pos);
    notifyitemremoved(pos);
}

5. 联系方式

qq:
email :
phone : 18684732678

总结

以上所述是小编给大家介绍的android 高仿微信朋友圈拍照上传功能,希望对大家有所帮助

上一篇:

下一篇: