Android實現微信朋友圈圖片和視頻播放
本文實例為大傢分享瞭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; } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。
推薦閱讀:
- Android圓形控件實現畫圓效果
- Android自定義view仿QQ的Tab按鈕動畫效果(示例代碼)
- Android自定義控件實現通用驗證碼輸入框(二)
- Android自定義View實現星星評分效果
- android實現多點觸摸效果