您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android如何實現微信朋友圈圖片和視頻播放,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
具體內容如下
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; } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android如何實現微信朋友圈圖片和視頻播放”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。