Android實(shí)現(xiàn)微信朋友圈圖片和視頻播放
本文實(shí)例為大家分享了Android實(shí)現(xiàn)微信朋友圈圖片和視頻播放的具體代碼,供大家參考,具體內(nèi)容如下
1.效果圖:
2.源碼地址:鏈接
3.參數(shù)控制,是否顯示播放按鈕
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; //根據(jù)寬高比例自動(dòng)計(jì)算高度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: 設(shè)置圖片類型,如果是TYPE_IMAGE,顯示圖片,如果是TYPE_VIDEO,顯示圖片,并且在圖片正中心繪制一個(gè)播放按鈕 public void setType(boolean type, int i, String url){ this.type = type; this.i = i; this.url = url; } //設(shè)置ImageView的寬高比 public void setRatio(float ratio) { mRatio = ratio; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if(type){ //如果是true,顯示圖片,并且在圖片正中心繪制一個(gè)播放按鈕 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){ //先根據(jù)比例縮放圖標(biāo),確保繪制的時(shí)候再次回歸縮放,保持播放的圖片大小不變 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) { //重新計(jì)算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: 根據(jù)傳入的圖片寬高,計(jì)算出最終的imageview的寬高,長(zhǎng)寬等比縮放 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) { //不進(jìn)行縮放 showWidth = w; showHeight = h; } else if (s > scale) { //寬取最大,高進(jìn)行縮小 showWidth = maxW; showHeight = (int) (h * (showWidth * 1.0 / w)); } else if (s <= scale) {//高取最大,寬進(jìn)行縮小 showHeight = maxH; showWidth = (int) (w * (showHeight * 1.0 / h)); } float[] result = new float[2]; result[0] = showWidth; result[1] = showHeight; return result; } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
android byte[] 和short[]轉(zhuǎn)換的方法代碼
這篇文章主要介紹了android byte[] 和short[]轉(zhuǎn)換的方法代碼,有需要的朋友可以參考一下2014-01-01Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容
這篇文章主要介紹了Android實(shí)現(xiàn)動(dòng)態(tài)顯示或隱藏密碼輸入框的內(nèi)容,主要通過(guò)設(shè)置EditText的setTransformationMethod()方法來(lái)實(shí)現(xiàn),需要的朋友可以參考下2014-09-09Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Android中asset文件夾與raw文件夾的區(qū)別深入解析
本篇文章是對(duì)Android中的asset文件夾與raw文件夾區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06Android實(shí)現(xiàn)從activity中停止Service的方法
這篇文章主要介紹了Android實(shí)現(xiàn)從activity中停止Service的方法,結(jié)合實(shí)例形式簡(jiǎn)單分析了Android中Service的注冊(cè)、創(chuàng)建及使用stopService停止Service的方法,需要的朋友可以參考下2016-01-01Android Eclipse 注釋模板的使用(圖文說(shuō)明)
為提高代碼的可讀性以及后期的可維護(hù)性,為我們的代碼加上規(guī)范化的注釋是很有必要,不僅有利于提高自己的專業(yè)素養(yǎng),也能方便他人2013-12-12Android 無(wú)障礙全局懸浮窗實(shí)現(xiàn)示例
本文主要介紹了Android 無(wú)障礙全局懸浮窗實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06android判斷phonegap是否聯(lián)網(wǎng)且加載super.loadUrl網(wǎng)址
android判斷phonegap是否聯(lián)網(wǎng)動(dòng)態(tài)加載super.loadUrl網(wǎng)址,接下來(lái)本文所提供的知識(shí)會(huì)幫助你解決以上問(wèn)題,感興趣的你可不要錯(cuò)過(guò)了哈2013-02-02Android 中RecyclerView通用適配器的實(shí)現(xiàn)
這篇文章主要介紹了Android 中RecyclerView通用適配器的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-03-03Android手勢(shì)滑動(dòng)實(shí)現(xiàn)兩點(diǎn)觸摸縮放圖片
這篇文章主要介紹了Android手勢(shì)滑動(dòng)實(shí)現(xiàn)兩點(diǎn)觸摸縮放圖片的相關(guān)資料,需要的朋友可以參考下2016-02-02