Android实现微信朋友圈图片和视频播放
程序员文章站
2022-08-05 11:02:50
本文实例为大家分享了android实现微信朋友圈图片和视频播放的具体代码,供大家参考,具体内容如下1.效果图:2.源码地址:链接3.参数控制,是否显示播放按钮holder.layout.setissh...
本文实例为大家分享了android实现微信朋友圈图片和视频播放的具体代码,供大家参考,具体内容如下
1.效果图:
2.源码地址:链接
3.参数控制,是否显示播放按钮
holder.layout.setisshowall(mlist.get(position).isshowall); holder.layout.setisvideo(true); //true :video, false :img holder.layout.seturllist(mlist.get(position).urllist);
4.自定义控件:
package com.example.mepositry.view; import android.content.context; import android.content.res.typedarray; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.porterduff; import android.graphics.rect; import android.graphics.rectf; import android.graphics.drawable.drawable; import android.support.v7.widget.appcompatimageview; import android.util.attributeset; import android.view.motionevent; import android.view.viewgroup; import com.example.mepositry.r; //根据宽高比例自动计算高度imageview public class ratioimageview extends appcompatimageview { private int playbtnres = r.mipmap.play_btn_video; private bitmap playbtnbitmap; private boolean type; //true表示video private int i; //i图片id private string url; //url图片地址 private paint mpaint = new paint(paint.anti_alias_flag); rect src = new rect(); rectf dest = new rectf(); //* 宽高比例 private float mratio = 0f; public ratioimageview(context context, attributeset attrs, int defstyleattr) { super(context, attrs, defstyleattr); } public ratioimageview(context context, attributeset attrs) { super(context, attrs); /* typedarray typedarray = context.obtainstyledattributes(attrs, r.styleable.ratioimageview); playbtnres = typedarray.getresourceid(r.styleable.imageviewplay_ivp_play_btn_res, playbtnres); playbtnbitmap = bitmapfactory.decoderesource(getresources(), playbtnres); mratio = typedarray.getfloat(r.styleable.ratioimageview_ratio, 0f); typedarray.recycle();*/ } public ratioimageview(context context) { super(context); typedarray typedarray = context.obtainstyledattributes(r.styleable.ratioimageview); playbtnres = typedarray.getresourceid(r.styleable.imageviewplay_ivp_play_btn_res, playbtnres); playbtnbitmap = bitmapfactory.decoderesource(getresources(), playbtnres); mratio = typedarray.getfloat(r.styleable.ratioimageview_ratio, 0f); typedarray.recycle(); } //*description: 设置图片类型,如果是type_image,显示图片,如果是type_video,显示图片,并且在图片正中心绘制一个播放按钮 public void settype(boolean type, int i, string url){ this.type = type; this.i = i; this.url = url; } //设置imageview的宽高比 public void setratio(float ratio) { mratio = ratio; } @override protected void ondraw(canvas canvas) { super.ondraw(canvas); if(type){ //如果是true,显示图片,并且在图片正中心绘制一个播放按钮 drawable drawable = getdrawable(); if (drawable != null) { int vieww = drawable.getintrinsicwidth(); //获取图片的宽 int viewh = drawable.getintrinsicheight(); //获取图片的高 int btnw = playbtnbitmap.getwidth(); //获取播放按钮的宽 int btnh = playbtnbitmap.getheight(); //获取播放按钮的高 float[] result = measureviewsize(vieww, viewh); if(result[0] > 0 && result[1] > 0){ //先根据比例缩放图标,确保绘制的时候再次回归缩放,保持播放的图片大小不变 btnw *= (vieww / result[0]); btnh *= (viewh / result[1]); } float left = (vieww - btnw) / 2.0f; float top = (viewh - btnh) / 2.0f; src.set(0, 0, btnw, btnh); dest.set(left, top, left+btnw, top+btnh); canvas.save(); canvas.concat(getimagematrix()); canvas.drawbitmap(playbtnbitmap, src, dest, mpaint); canvas.restore(); } } } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { int width = measurespec.getsize(widthmeasurespec); drawable drawable = getdrawable(); if (drawable != null) { //重新计算view int vieww = drawable.getintrinsicwidth(); int viewh = drawable.getintrinsicheight(); if(vieww > 0 && viewh > 0) { float[] result = measureviewsize(vieww, viewh); setmeasureddimension((int)result[0], (int) result[1]); } } if (mratio != 0) { float height = width / mratio; heightmeasurespec = measurespec.makemeasurespec((int) height, measurespec.exactly); } super.onmeasure(widthmeasurespec, heightmeasurespec); } @override public boolean ontouchevent(motionevent event) { switch (event.getaction()) { case motionevent.action_down: drawable drawable = getdrawable(); if (drawable != null) { drawable.mutate().setcolorfilter(color.gray, porterduff.mode.multiply); } break; case motionevent.action_move: break; case motionevent.action_cancel: case motionevent.action_up: drawable drawableup = getdrawable(); if (drawableup != null) { drawableup.mutate().clearcolorfilter(); } break; } return super.ontouchevent(event); } // *description: 根据传入的图片宽高,计算出最终的imageview的宽高,长宽等比缩放 private float[] measureviewsize(int w, int h) { viewgroup.layoutparams lp = getlayoutparams(); float maxw = lp.width; float maxh = lp.height; float showwidth = w; float showheight = h; float scale = (1.0f * maxw) / maxh; float s = 1.0f * w / h; if (w < maxw && h < maxh) { //不进行缩放 showwidth = w; showheight = h; } else if (s > scale) { //宽取最大,高进行缩小 showwidth = maxw; showheight = (int) (h * (showwidth * 1.0 / w)); } else if (s <= scale) {//高取最大,宽进行缩小 showheight = maxh; showwidth = (int) (w * (showheight * 1.0 / h)); } float[] result = new float[2]; result[0] = showwidth; result[1] = showheight; return result; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: 药物牙膏不能治疗口腔炎吗