免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1643 | 回复: 1
打印 上一主题 下一主题

[Android] 【高仿微信系列】微信录制小视频 [复制链接]

论坛徽章:
1
操作系统版块每日发帖之星
日期:2015-06-19 22:20:00
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-07-16 13:36 |只看该作者 |倒序浏览
微信从6.0版本开始推出小视频功能,随着4G网络的出现,视频将会是一个趋势,他能表达出文字所不能表现的东西,增加了微信的黏性。还记得微信小视频这个功能一推出,如同病毒一样席卷朋友圈。
看看微信小视频下滑眼镜效果,还有视频录制完,类似美拍,添加各种滤镜,炫酷MV主题,美化视频...

项目的完整代码可以去 Github (点击这里) 下载
https://github.com/motianhuo/VCameraDemo



[Java]代码
  1. package com.example.wechat01.widght;

  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.util.AttributeSet;
  5. import android.util.Log;
  6. import android.view.MotionEvent;
  7. import android.view.View;
  8. import android.widget.AbsListView;
  9. import android.widget.AbsListView.OnScrollListener;
  10. import android.widget.ListView;
  11. import android.widget.RelativeLayout;

  12. import com.example.wechat01.R;
  13. import com.nineoldandroids.animation.ValueAnimator;
  14. import com.nineoldandroids.animation.ValueAnimator.AnimatorUpdateListener;

  15. public class PullDownListView extends RelativeLayout implements
  16.                 OnScrollListener {
  17.         static int MAX_PULL_TOP_HEIGHT;
  18.         static int MAX_PULL_BOTTOM_HEIGHT;

  19.         static int REFRESHING_TOP_HEIGHT;
  20.         static int REFRESHING_BOTTOM_HEIGHT;

  21.         private boolean isTop;
  22.         private boolean isBottom;
  23.         private boolean isRefreshing;
  24.         private boolean isAnimation;

  25.         RelativeLayout layoutHeader;
  26.         RelativeLayout layoutFooter;

  27.         private int mCurrentY = 0;
  28.         boolean pullTag = false;
  29.         OnScrollListener mOnScrollListener;
  30.         OnPullHeightChangeListener mOnPullHeightChangeListener;

  31.         public void setOnPullHeightChangeListener(
  32.                         OnPullHeightChangeListener listener) {
  33.                 this.mOnPullHeightChangeListener = listener;
  34.         }

  35.         public void setOnScrollListener(OnScrollListener listener) {
  36.                 mOnScrollListener = listener;
  37.         }

  38.         public PullDownListView(Context context, AttributeSet attrs) {
  39.                 super(context, attrs);
  40.                 // TODO Auto-generated constructor stub
  41.         }

  42.         public boolean isRefreshing() {
  43.                 return this.isRefreshing;
  44.         }

  45.         private ListView mListView = new ListView(getContext()) {

  46.                 int lastY = 0;

  47.                 @Override
  48.                 public boolean onTouchEvent(MotionEvent ev) {
  49.                         if (isAnimation || isRefreshing) {
  50.                                 return super.onTouchEvent(ev);
  51.                         }
  52.                         RelativeLayout parent = (RelativeLayout) mListView.getParent();

  53.                         int currentY = (int) ev.getRawY();
  54.                         switch (ev.getAction()) {
  55.                         case MotionEvent.ACTION_DOWN:
  56.                                 lastY = (int) ev.getRawY();
  57.                                 break;
  58.                         case MotionEvent.ACTION_MOVE: {
  59.                                 boolean isToBottom = currentY - lastY >= 0 ? true : false;

  60.                                 int step = Math.abs(currentY - lastY);
  61.                                 lastY = currentY;

  62.                                 if (isTop && mListView.getTop() >= 0) {

  63.                                         if (isToBottom && mListView.getTop() <= MAX_PULL_TOP_HEIGHT) {
  64.                                                 MotionEvent event = MotionEvent.obtain(ev);
  65.                                                 ev.setAction(MotionEvent.ACTION_UP);
  66.                                                 super.onTouchEvent(ev);
  67.                                                 pullTag = true;

  68.                                                 if (mListView.getTop() > layoutHeader.getHeight()) {
  69.                                                         step = step / 2;
  70.                                                 }
  71.                                                 if ((mListView.getTop() + step) > MAX_PULL_TOP_HEIGHT) {
  72.                                                         mCurrentY = MAX_PULL_TOP_HEIGHT;
  73.                                                         scrollTopTo(mCurrentY);
  74.                                                 } else {
  75.                                                         mCurrentY += step;
  76.                                                         scrollTopTo(mCurrentY);
  77.                                                 }
  78.                                         } else if (!isToBottom && mListView.getTop() > 0) {
  79.                                                 MotionEvent event = MotionEvent.obtain(ev);
  80.                                                 ev.setAction(MotionEvent.ACTION_UP);
  81.                                                 super.onTouchEvent(ev);
  82.                                                 if ((mListView.getTop() - step) < 0) {
  83.                                                         mCurrentY = 0;
  84.                                                         scrollTopTo(mCurrentY);
  85.                                                 } else {
  86.                                                         mCurrentY -= step;
  87.                                                         scrollTopTo(mCurrentY);
  88.                                                 }
  89.                                         } else if (!isToBottom && mListView.getTop() == 0) {
  90.                                                 if (!pullTag) {
  91.                                                         return super.onTouchEvent(ev);
  92.                                                 }

  93.                                         }

  94.                                         return true;
  95.                                 } else if (isBottom
  96.                                                 && mListView.getBottom() <= parent.getHeight()) {
  97.                                         if (!isToBottom
  98.                                                         && (parent.getHeight() - mListView.getBottom()) <= MAX_PULL_BOTTOM_HEIGHT) {
  99.                                                 MotionEvent event = MotionEvent.obtain(ev);
  100.                                                 ev.setAction(MotionEvent.ACTION_UP);
  101.                                                 super.onTouchEvent(ev);
  102.                                                 pullTag = true;
  103.                                                 if (parent.getHeight() - mListView.getBottom() > layoutFooter
  104.                                                                 .getHeight()) {
  105.                                                         step = step / 2;
  106.                                                 }

  107.                                                 if ((mListView.getBottom() - step) < (parent
  108.                                                                 .getHeight() - MAX_PULL_BOTTOM_HEIGHT)) {
  109.                                                         mCurrentY = -MAX_PULL_BOTTOM_HEIGHT;
  110.                                                         scrollBottomTo(mCurrentY);
  111.                                                 } else {
  112.                                                         mCurrentY -= step;
  113.                                                         scrollBottomTo(mCurrentY);
  114.                                                 }
  115.                                         } else if (isToBottom
  116.                                                         && (mListView.getBottom() < parent.getHeight())) {
  117.                                                 if ((mListView.getBottom() + step) > parent.getHeight()) {
  118.                                                         mCurrentY = 0;
  119.                                                         scrollBottomTo(mCurrentY);
  120.                                                 } else {
  121.                                                         mCurrentY += step;
  122.                                                         scrollBottomTo(mCurrentY);
  123.                                                 }
  124.                                         } else if (isToBottom
  125.                                                         && mListView.getBottom() == parent.getHeight()) {
  126.                                                 if (!pullTag) {
  127.                                                         return super.onTouchEvent(ev);
  128.                                                 }
  129.                                         }
  130.                                         return true;
  131.                                 }
  132.                                 break;
  133.                         }
  134.                         case MotionEvent.ACTION_CANCEL:
  135.                         case MotionEvent.ACTION_UP:
  136.                                 pullTag = false;

  137.                                 if (mListView.getTop() > 0) {
  138.                                         if (mListView.getTop() > REFRESHING_TOP_HEIGHT) {
  139.                                                 animateTopTo(layoutHeader.getMeasuredHeight());
  140.                                                 isRefreshing = true;
  141.                                                 if (null != mOnPullHeightChangeListener) {
  142.                                                         mOnPullHeightChangeListener.onRefreshing(true);
  143.                                                 }
  144.                                         } else {
  145.                                                 animateTopTo(0);
  146.                                         }

  147.                                 } else if (mListView.getBottom() < parent.getHeight()) {
  148.                                         if ((parent.getHeight() - mListView.getBottom()) > REFRESHING_BOTTOM_HEIGHT) {
  149.                                                 animateBottomTo(-layoutFooter.getMeasuredHeight());
  150.                                                 isRefreshing = true;
  151.                                                 if (null != mOnPullHeightChangeListener) {
  152.                                                         mOnPullHeightChangeListener.onRefreshing(false);
  153.                                                 }
  154.                                         } else {
  155.                                                 animateBottomTo(0);
  156.                                         }
  157.                                 }

  158.                         }

  159.                         return super.onTouchEvent(ev);
  160.                 }

  161.         };

  162.         public void scrollBottomTo(int y) {
  163.                 mListView.layout(mListView.getLeft(), y, mListView.getRight(),
  164.                                 this.getMeasuredHeight() + y);
  165.                 if (null != mOnPullHeightChangeListener) {
  166.                         mOnPullHeightChangeListener.onBottomHeightChange(
  167.                                         layoutHeader.getHeight(), -y);
  168.                 }
  169.         }

  170.         public void animateBottomTo(final int y) {
  171.                 ValueAnimator animator = ValueAnimator.ofInt(mListView.getBottom()
  172.                                 - this.getMeasuredHeight(), y);
  173.                 animator.setDuration(300);
  174.                 animator.addUpdateListener(new AnimatorUpdateListener() {
  175.                         @Override
  176.                         public void onAnimationUpdate(ValueAnimator animation) {
  177.                                 // TODO Auto-generated method stub
  178.                                 int frameValue = (Integer) animation.getAnimatedValue();
  179.                                 mCurrentY = frameValue;
  180.                                 scrollBottomTo(frameValue);
  181.                                 if (frameValue == y) {
  182.                                         isAnimation = false;
  183.                                 }
  184.                         }
  185.                 });
  186.                 isAnimation = true;
  187.                 animator.start();
  188.         }

  189.         public void scrollTopTo(int y) {
  190.                 mListView.layout(mListView.getLeft(), y, mListView.getRight(),
  191.                                 this.getMeasuredHeight() + y);
  192.                 if (null != mOnPullHeightChangeListener) {
  193.                         mOnPullHeightChangeListener.onTopHeightChange(
  194.                                         layoutHeader.getHeight(), y);
  195.                 }
  196.         }

  197.         public void animateTopTo(final int y) {
  198.                 ValueAnimator animator = ValueAnimator.ofInt(mListView.getTop(), y);
  199.                 animator.setDuration(300);
  200.                 animator.addUpdateListener(new AnimatorUpdateListener() {
  201.                         @Override
  202.                         public void onAnimationUpdate(ValueAnimator animation) {
  203.                                 // TODO Auto-generated method stub
  204.                                 int frameValue = (Integer) animation.getAnimatedValue();
  205.                                 mCurrentY = frameValue;
  206.                                 scrollTopTo(frameValue);
  207.                                 if (frameValue == y) {
  208.                                         isAnimation = false;
  209.                                 }
  210.                         }
  211.                 });
  212.                 isAnimation = true;
  213.                 animator.start();
  214.         }

  215.         @Override
  216.         public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  217.                 super.onMeasure(widthMeasureSpec, heightMeasureSpec);

  218.                 REFRESHING_TOP_HEIGHT = layoutHeader.getMeasuredHeight();
  219.                 REFRESHING_BOTTOM_HEIGHT = layoutFooter.getMeasuredHeight();

  220.                 MAX_PULL_TOP_HEIGHT = this.getMeasuredHeight();
  221.                 MAX_PULL_BOTTOM_HEIGHT = this.getMeasuredHeight();
  222.         }

  223.         @Override
  224.         public void onFinishInflate() {

  225.                 mListView.setBackgroundColor(0xffffffff);
  226.                 mListView.setCacheColorHint(Color.TRANSPARENT);
  227.                 mListView.setVerticalScrollBarEnabled(false);
  228.                 mListView.setLayoutParams(new RelativeLayout.LayoutParams(
  229.                                 LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  230.                 mListView.setOnScrollListener(this);
  231.                 mListView.setDividerHeight(0);
  232.                 this.addView(mListView);

  233.                 layoutHeader = (RelativeLayout) this.findViewById(R.id.layoutHeader);
  234.                 layoutFooter = (RelativeLayout) this.findViewById(R.id.layoutFooter);

  235.                 super.onFinishInflate();
  236.         }

  237.         public ListView getListView() {
  238.                 return this.mListView;
  239.         }

  240.         public void pullUp() {
  241.                 isRefreshing = false;
  242.                 if (mListView.getTop() > 0) {
  243.                         animateTopTo(0);
  244.                 } else if (mListView.getBottom() < this.getHeight()) {
  245.                         animateBottomTo(0);
  246.                 }

  247.         }

  248.         @Override
  249.         public void onScroll(AbsListView view, int firstVisibleItem,
  250.                         int visibleItemCount, int totalItemCount) {
  251.                 // TODO Auto-generated method stub
  252.                 if (null != mOnScrollListener) {
  253.                         mOnScrollListener.onScroll(view, firstVisibleItem,
  254.                                         visibleItemCount, totalItemCount);
  255.                 }
  256.                 if (mListView.getCount() > 0) {
  257.                         if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
  258.                                 View lastItem = (View) mListView
  259.                                                 .getChildAt(visibleItemCount - 1);
  260.                                 if (null != lastItem) {

  261.                                         if (lastItem.getBottom() == mListView.getHeight()) {
  262.                                                 Log.e("my", lastItem.getBottom() + "");
  263.                                                 isBottom = true;
  264.                                         } else {
  265.                                                 isBottom = false;
  266.                                         }
  267.                                 }
  268.                         } else {
  269.                                 isBottom = false;
  270.                         }
  271.                 } else {
  272.                         isBottom = false;
  273.                 }

  274.                 if (mListView.getCount() > 0) {
  275.                         if (firstVisibleItem == 0) {
  276.                                 View firstItem = mListView.getChildAt(0);
  277.                                 if (null != firstItem) {
  278.                                         if (firstItem.getTop() == 0) {
  279.                                                 isTop = true;
  280.                                         } else {
  281.                                                 isTop = false;
  282.                                         }
  283.                                 }
  284.                         } else {
  285.                                 isTop = false;
  286.                         }
  287.                 } else {
  288.                         isTop = true;
  289.                 }

  290.         }

  291.         @Override
  292.         public void onScrollStateChanged(AbsListView view, int scrollState) {
  293.                 // TODO Auto-generated method stub
  294.                 if (null != mOnScrollListener) {
  295.                         mOnScrollListener.onScrollStateChanged(view, scrollState);
  296.                 }
  297.         }

  298.         // listener call back
  299.         public interface OnPullHeightChangeListener {
  300.                 public void onTopHeightChange(int headerHeight, int pullHeight);

  301.                 public void onBottomHeightChange(int footerHeight, int pullHeight);

  302.                 public void onRefreshing(boolean isTop);
  303.         }
  304. }
复制代码

论坛徽章:
80
20周年集字徽章-庆
日期:2020-10-28 14:09:1215-16赛季CBA联赛之北京
日期:2020-10-28 13:32:5315-16赛季CBA联赛之北控
日期:2020-10-28 13:32:4815-16赛季CBA联赛之天津
日期:2020-10-28 13:13:35黑曼巴
日期:2020-10-28 12:29:1520周年集字徽章-周	
日期:2020-10-31 15:10:0720周年集字徽章-20	
日期:2020-10-31 15:10:07ChinaUnix元老
日期:2015-09-29 11:56:3020周年集字徽章-年
日期:2020-10-28 14:14:56
2 [报告]
发表于 2015-07-16 16:24 |只看该作者
谢谢 大牛的分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP