您好,登錄后才能下訂單哦!
這篇文章主要介紹Android自定義videoview仿抖音界面的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動設(shè)備,如智能手機和平板電腦,由美國Google公司和開放手機聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
具體內(nèi)容如下
1.效果圖
和抖音的界面效果一模一樣,而且可以自定義,需要什么頁面,請自己定義
2.自定義videoview
package com.example.myapplication20; import android.content.Context; import android.util.AttributeSet; import android.widget.VideoView; public class CusVideoView extends VideoView { public CusVideoView(Context context) { super(context); } public CusVideoView(Context context, AttributeSet attrs) { super(context, attrs); } public CusVideoView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = getDefaultSize(getWidth(), widthMeasureSpec); int height = getDefaultSize(getHeight(), heightMeasureSpec); setMeasuredDimension(width, height); } }
3.xml界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/mRootView" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/mThumb" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="false" android:focusable="false" android:scaleType="centerCrop" android:visibility="visible" /> <ImageView android:id="@+id/mPlay" android:layout_width="100dp" android:layout_height="100dp" android:layout_centerInParent="true" android:alpha="0" android:clickable="true" android:focusable="true" android:src="@drawable/play_arrow" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_marginLeft="10dp" android:layout_marginBottom="60dp" android:orientation="vertical"> <TextView android:id="@+id/mTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:lineSpacingExtra="5dp" android:textColor="@android:color/white" android:textSize="16sp" tools:text="測試測試數(shù)據(jù)哈哈哈哈\n家里幾個垃圾了個兩個垃圾" /> </LinearLayout> <com.example.myapplication20.CusVideoView android:id="@+id/mVideoView" android:layout_width="match_parent" android:layout_height="match_parent" android:clickable="false" android:focusable="false" /> </RelativeLayout>
4.drawable
<vector android:alpha="0.61" android:height="24dp" android:viewportHeight="24.0" android:viewportWidth="24.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> <path android:fillColor="#99ffffff" android:pathData="M8,5v14l11,-7z"/> </vector>
5.主界面設(shè)置地址,注意,本demo使用的是本地的視頻文件,文件存儲再../res/raw文件夾里面,請自行獲取
package com.example.myapplication20; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; /** * 作者:JArchie * 源碼參考地址:https://github.com/JArchie/TiktokDemo */ public class MainActivity extends AppCompatActivity { CusVideoView mVideoView; private int[] videos = {R.raw.v1, R.raw.v2, R.raw.qi}; TextView mTitle; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mVideoView = findViewById(R.id.mVideoView); mTitle = findViewById(R.id.mTitle); String url = "android.resource://" + getPackageName() + "/" + videos[1]; Log.e("TAG", "video_onCreate: " + url); mVideoView.setVideoURI(Uri.parse(url)); mTitle.setText("@王燕\n一起來跳支舞吧"); } @Override protected void onStart() { super.onStart(); playVideo(); } @Override protected void onDestroy() { super.onDestroy(); releaseVideo(); } //播放 private void playVideo() { Log.e("TAG", "play_video"); // View itemView = mRecycler.getChildAt(0); final CusVideoView mVideoView = findViewById(R.id.mVideoView); final ImageView mPlay = findViewById(R.id.mPlay); final ImageView mThumb = findViewById(R.id.mThumb); final MediaPlayer[] mMediaPlayer = new MediaPlayer[1]; mVideoView.start(); mVideoView.setOnInfoListener(new MediaPlayer.OnInfoListener() { @Override public boolean onInfo(MediaPlayer mp, int what, int extra) { mMediaPlayer[0] = mp; mp.setLooping(true); mThumb.animate().alpha(0).setDuration(200).start(); return false; } }); //暫??刂? mPlay.setOnClickListener(new View.OnClickListener() { boolean isPlaying = true; @Override public void onClick(View v) { if (mVideoView.isPlaying()) { mPlay.animate().alpha(1f).start(); mVideoView.pause(); isPlaying = false; } else { mPlay.animate().alpha(0f).start(); mVideoView.start(); isPlaying = true; } } }); } //釋放 private void releaseVideo() { Log.e("TAG", "releaseVideo_video"); // View itemView = mRecycler.getChildAt(index); final CusVideoView mVideoView = findViewById(R.id.mVideoView); final ImageView mThumb = findViewById(R.id.mThumb); final ImageView mPlay = findViewById(R.id.mPlay); mVideoView.stopPlayback(); mThumb.animate().alpha(1).start(); mPlay.animate().alpha(0f).start(); } }
以上是“Android自定義videoview仿抖音界面的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。