我的编程空间,编程开发者的网络收藏夹
学习永远不晚

Android VideoView类实例讲解

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

Android VideoView类实例讲解

        本节使用系统的示例类VideoView继续SurfaceView类相关内容的讲解,以让大家能更深入理解Android系统中图形绘制基础类的实现原理。也许你会发现无法改变VideoView类的控制方面,我们可以通过重构VideoView类来实现更加个性化的播放器。

         下面是VideoView类的相关代码。

Java 代码


 public class VideoView extends SurfaceView implements MediaPlayerControl { 
 private String TAG = "VideoView"; 
 // settable by the client 
 private Uri   mUri; 
 private int   mDuration; 
 // all possible internal states 
 private static final int STATE_ERROR    = -1; 
 private static final int STATE_IDLE    = 0; 
 private static final int STATE_PREPARING   = 1; 
 private static final int STATE_PREPARED   = 2; 
 private static final int STATE_PLAYING   = 3; 
 private static final int STATE_PAUSED    = 4; 
 private static final int STATE_PLAYBACK_COMPLETED = 5; 
 // mCurrentState is a VideoView object's current state. 
 // mTargetState is the state that a method caller intends to reach. 
 // For instance, regardless the VideoView object's current state, 
 // calling pause() intends to bring the object to a target state 
 // of STATE_PAUSED. 
 private int mCurrentState = STATE_IDLE; 
 private int mTargetState = STATE_IDLE; 
 // All the stuff we need for playing and showing a video 
 private SurfaceHolder mSurfaceHolder = null; 
 private MediaPlayer mMediaPlayer = null; 
 private int   mVideoWidth; 
 private int   mVideoHeight; 
 private int   mSurfaceWidth; 
 private int   mSurfaceHeight; 
 private MediaController mMediaController; 
 private OnCompletionListener mOnCompletionListener; 
 private MediaPlayer.OnPreparedListener mOnPreparedListener; 
 private int   mCurrentBufferPercentage; 
 private OnErrorListener mOnErrorListener; 
 private int   mSeekWhenPrepared; // recording the seek position while preparing 
 private boolean  mCanPause; 
 private boolean  mCanSeekBack; 
 private boolean  mCanSeekForward; 
 public VideoView(Context context) { 
  super(context); 
  initVideoView(); 
 } 
 public VideoView(Context context, AttributeSet attrs) { 
  this(context, attrs, 0); 
  initVideoView(); 
 } 
 public VideoView(Context context, AttributeSet attrs, int defStyle) { 
  super(context, attrs, defStyle); 
  initVideoView(); 
 } 
 @Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  //Log.i("@@@@", "onMeasure"); 
  int width = getDefaultSize(mVideoWidth, widthMeasureSpec); 
  int height = getDefaultSize(mVideoHeight, heightMeasureSpec); 
  if (mVideoWidth > 0 && mVideoHeight > 0) { 
   if ( mVideoWidth * height > width * mVideoHeight ) { 
    //Log.i("@@@", "image too tall, correcting"); 
    height = width * mVideoHeight / mVideoWidth; 
   } else if ( mVideoWidth * height < width * mVideoHeight ) { 
    //Log.i("@@@", "image too wide, correcting"); 
    width = height * mVideoWidth / mVideoHeight; 
   } else { 
    //Log.i("@@@", "aspect ratio is correct: " + 
      //width+"/"+height+"="+ 
      //mVideoWidth+"/"+mVideoHeight); 
   } 
  } 
  //Log.i("@@@@@@@@@@", "setting size: " + width + 'x' + height); 
  setMeasuredDimension(width, height); 
 } 
 public int resolveAdjustedSize(int desiredSize, int measureSpec) { 
  int result = desiredSize; 
  int specMode = MeasureSpec.getMode(measureSpec); 
  int specSize = MeasureSpec.getSize(measureSpec); 
  switch (specMode) { 
   case MeasureSpec.UNSPECIFIED: 
        result = desiredSize; 
    break; 
   case MeasureSpec.AT_MOST: 
     
    result = Math.min(desiredSize, specSize); 
    break; 
   case MeasureSpec.EXACTLY: 
    // No choice. Do what we are told. 
    result = specSize; 
    break; 
  } 
  return result; 
} 
 private void initVideoView() { 
  mVideoWidth = 0; 
  mVideoHeight = 0; 
  getHolder().addCallback(mSHCallback); 
  getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
  setFocusable(true); 
  setFocusableInTouchMode(true); 
  requestFocus(); 
  mCurrentState = STATE_IDLE; 
  mTargetState = STATE_IDLE; 
 } 
 public void setVideoPath(String path) { 
  setVideoURI(Uri.parse(path)); 
 } 
 public void setVideoURI(Uri uri) { 
  mUri = uri; 
  mSeekWhenPrepared = 0; 
  openVideo(); 
  requestLayout(); 
  invalidate(); 
 } 
 public void stopPlayback() { 
  if (mMediaPlayer != null) { 
   mMediaPlayer.stop(); 
   mMediaPlayer.release(); 
   mMediaPlayer = null; 
   mCurrentState = STATE_IDLE; 
   mTargetState = STATE_IDLE; 
  } 
 } 
 private void openVideo() { 
  if (mUri == null || mSurfaceHolder == null) { 
   // not ready for playback just yet, will try again later 
   return; 
  } 
  // Tell the music playback service to pause 
  // TODO: these constants need to be published somewhere in the framework. 
  Intent i = new Intent("com.android.music.musicservicecommand"); 
  i.putExtra("command", "pause"); 
  mContext.sendBroadcast(i); 
  // we shouldn't clear the target state, because somebody might have 
  // called start() previously 
  release(false); 
  try { 
   mMediaPlayer = new MediaPlayer(); 
   mMediaPlayer.setOnPreparedListener(mPreparedListener); 
   mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener); 
   mDuration = -1; 
   mMediaPlayer.setOnCompletionListener(mCompletionListener); 
   mMediaPlayer.setOnErrorListener(mErrorListener); 
   mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener); 
   mCurrentBufferPercentage = 0; 
   mMediaPlayer.setDataSource(mContext, mUri); 
   mMediaPlayer.setDisplay(mSurfaceHolder); 
   mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); 
   mMediaPlayer.setScreenOnWhilePlaying(true); 
   mMediaPlayer.prepareAsync(); 
   // we don't set the target state here either, but preserve the 
   // target state that was there before. 
   mCurrentState = STATE_PREPARING; 
   attachMediaController(); 
  } catch (IOException ex) { 
   Log.w(TAG, "Unable to open content: " + mUri, ex); 
   mCurrentState = STATE_ERROR; 
   mTargetState = STATE_ERROR; 
   mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); 
   return; 
  } catch (IllegalArgumentException ex) { 
   Log.w(TAG, "Unable to open content: " + mUri, ex); 
   mCurrentState = STATE_ERROR; 
   mTargetState = STATE_ERROR; 
   mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0); 
   return; 
  } 
 } 
 public void setMediaController(MediaController controller) { 
  if (mMediaController != null) { 
   mMediaController.hide(); 
  } 
  mMediaController = controller; 
  attachMediaController(); 
 } 
 private void attachMediaController() { 
  if (mMediaPlayer != null && mMediaController != null) { 
   mMediaController.setMediaPlayer(this); 
   View anchorView = this.getParent() instanceof View ? 
     (View)this.getParent() : this; 
   mMediaController.setAnchorView(anchorView); 
   mMediaController.setEnabled(isInPlaybackState()); 
  } 
 } 
 MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener = 
  new MediaPlayer.OnVideoSizeChangedListener() { 
   public void onVideoSizeChanged(MediaPlayer mp, int width, int height) { 
    mVideoWidth = mp.getVideoWidth(); 
    mVideoHeight = mp.getVideoHeight(); 
    if (mVideoWidth != 0 && mVideoHeight != 0) { 
     getHolder().setFixedSize(mVideoWidth, mVideoHeight); 
    } 
   } 
 }; 
 MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() { 
  public void onPrepared(MediaPlayer mp) { 
   mCurrentState = STATE_PREPARED; 
   // Get the capabilities of the player for this stream 
   Metadata data = mp.getMetadata(MediaPlayer.METADATA_ALL, 
          MediaPlayer.BYPASS_METADATA_FILTER); 
   if (data != null) { 
    mCanPause = !data.has(Metadata.PAUSE_AVAILABLE) 
      || data.getBoolean(Metadata.PAUSE_AVAILABLE); 
    mCanSeekBack = !data.has(Metadata.SEEK_BACKWARD_AVAILABLE) 
      || data.getBoolean(Metadata.SEEK_BACKWARD_AVAILABLE); 
    mCanSeekForward = !data.has(Metadata.SEEK_FORWARD_AVAILABLE) 
      || data.getBoolean(Metadata.SEEK_FORWARD_AVAILABLE); 
   } else { 
    mCanPause = mCanSeekForward = mCanSeekForward = true; 
   } 
   if (mOnPreparedListener != null) { 
    mOnPreparedListener.onPrepared(mMediaPlayer); 
   } 
   if (mMediaController != null) { 
    mMediaController.setEnabled(true); 
   } 
   mVideoWidth = mp.getVideoWidth(); 
   mVideoHeight = mp.getVideoHeight(); 
   int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be changed after seekTo() call 
   if (seekToPosition != 0) { 
    seekTo(seekToPosition); 
   } 
   if (mVideoWidth != 0 && mVideoHeight != 0) { 
    //Log.i("@@@@", "video size: " + mVideoWidth +"/"+ mVideoHeight); 
    getHolder().setFixedSize(mVideoWidth, mVideoHeight); 
    if (mSurfaceWidth == mVideoWidth && mSurfaceHeight == mVideoHeight) { 
     // We didn't actually change the size (it was already at the size 
     // we need), so we won't get a "surface changed" callback, so 
     // start the video here instead of in the callback. 
     if (mTargetState == STATE_PLAYING) { 
      start(); 
      if (mMediaController != null) { 
       mMediaController.show(); 
      } 
     } else if (!isPlaying() && 
        (seekToPosition != 0 || getCurrentPosition() > 0)) { 
      if (mMediaController != null) { 
       // Show the media controls when we're paused into a video and make 'em stick. 
       mMediaController.show(0); 
      } 
     } 
    } 
   } else { 
    // We don't know the video size yet, but should start anyway. 
    // The video size might be reported to us later. 
    if (mTargetState == STATE_PLAYING) { 
     start(); 
    } 
   } 
  } 
 }; 
 private MediaPlayer.OnCompletionListener mCompletionListener = 
  new MediaPlayer.OnCompletionListener() { 
  public void onCompletion(MediaPlayer mp) { 
   mCurrentState = STATE_PLAYBACK_COMPLETED; 
   mTargetState = STATE_PLAYBACK_COMPLETED; 
   if (mMediaController != null) { 
    mMediaController.hide(); 
   } 
   if (mOnCompletionListener != null) { 
    mOnCompletionListener.onCompletion(mMediaPlayer); 
   } 
  } 
 }; 
 private MediaPlayer.OnErrorListener mErrorListener = 
  new MediaPlayer.OnErrorListener() { 
  public boolean onError(MediaPlayer mp, int framework_err, int impl_err) { 
   Log.d(TAG, "Error: " + framework_err + "," + impl_err); 
   mCurrentState = STATE_ERROR; 
   mTargetState = STATE_ERROR; 
   if (mMediaController != null) { 
    mMediaController.hide(); 
   } 
    
   if (mOnErrorListener != null) { 
    if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) { 
     return true; 
    } 
   } 
    
   if (getWindowToken() != null) { 
    Resources r = mContext.getResources(); 
    int messageId; 
    if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) { 
     messageId = com.android.internal.R.string.VideoView_error_text_invalid_progressive_playback; 
    } else { 
     messageId = com.android.internal.R.string.VideoView_error_text_unknown; 
    } 
    new AlertDialog.Builder(mContext) 
      .setTitle(com.android.internal.R.string.VideoView_error_title) 
      .setMessage(messageId) 
      .setPositiveButton(com.android.internal.R.string.VideoView_error_button, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
           
          if (mOnCompletionListener != null) { 
           mOnCompletionListener.onCompletion(mMediaPlayer); 
          } 
         } 
        }) 
      .setCancelable(false) 
      .show(); 
   } 
   return true; 
  } 
 }; 
 private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener = 
  new MediaPlayer.OnBufferingUpdateListener() { 
  public void onBufferingUpdate(MediaPlayer mp, int percent) { 
   mCurrentBufferPercentage = percent; 
  } 
 }; 
  
 public void setOnPreparedListener(MediaPlayer.OnPreparedListener l) 
 { 
  mOnPreparedListener = l; 
 } 
  
 public void setOnCompletionListener(OnCompletionListener l) 
 { 
  mOnCompletionListener = l; 
 } 
  
 public void setOnErrorListener(OnErrorListener l) 
 { 
  mOnErrorListener = l; 
 } 
 SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback() 
 { 
  public void surfaceChanged(SurfaceHolder holder, int format, 
         int w, int h) 
  { 
   mSurfaceWidth = w; 
   mSurfaceHeight = h; 
   boolean isValidState = (mTargetState == STATE_PLAYING); 
   boolean hasValidSize = (mVideoWidth == w && mVideoHeight == h); 
   if (mMediaPlayer != null && isValidState && hasValidSize) { 
    if (mSeekWhenPrepared != 0) { 
     seekTo(mSeekWhenPrepared); 
    } 
    start(); 
    if (mMediaController != null) { 
     mMediaController.show(); 
    } 
   } 
  } 
  public void surfaceCreated(SurfaceHolder holder) 
  { 
   mSurfaceHolder = holder; 
   openVideo(); 
  } 
  public void surfaceDestroyed(SurfaceHolder holder) 
  { 
   // after we return from this we can't use the surface any more 
   mSurfaceHolder = null; 
   if (mMediaController != null) mMediaController.hide(); 
   release(true); 
  } 
 }; 
  private void release(boolean cleartargetstate) { 
  if (mMediaPlayer != null) { 
   mMediaPlayer.reset(); 
   mMediaPlayer.release(); 
   mMediaPlayer = null; 
   mCurrentState = STATE_IDLE; 
   if (cleartargetstate) { 
    mTargetState = STATE_IDLE; 
   } 
  } 
 } 
 @Override 
 public boolean onTouchEvent(MotionEvent ev) { 
  if (isInPlaybackState() && mMediaController != null) { 
   toggleMediaControlsVisiblity(); 
  } 
  return false; 
 } 
 @Override 
 public boolean onTrackballEvent(MotionEvent ev) { 
  if (isInPlaybackState() && mMediaController != null) { 
   toggleMediaControlsVisiblity(); 
  } 
  return false; 
 } 
 @Override 
 public boolean onKeyDown(int keyCode, KeyEvent event) 
 { 
  boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK && 
          keyCode != KeyEvent.KEYCODE_VOLUME_UP && 
          keyCode != KeyEvent.KEYCODE_VOLUME_DOWN && 
          keyCode != KeyEvent.KEYCODE_MENU && 
          keyCode != KeyEvent.KEYCODE_CALL && 
          keyCode != KeyEvent.KEYCODE_ENDCALL; 
  if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) { 
   if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK || 
     keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) { 
    if (mMediaPlayer.isPlaying()) { 
     pause(); 
     mMediaController.show(); 
    } else { 
     start(); 
     mMediaController.hide(); 
    } 
    return true; 
   } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP 
     && mMediaPlayer.isPlaying()) { 
    pause(); 
    mMediaController.show(); 
   } else { 
    toggleMediaControlsVisiblity(); 
   } 
  } 
  return super.onKeyDown(keyCode, event); 
 } 
 private void toggleMediaControlsVisiblity() { 
  if (mMediaController.isShowing()) { 
   mMediaController.hide(); 
  } else { 
   mMediaController.show(); 
  } 
 } 
 public void start() { 
  if (isInPlaybackState()) { 
   mMediaPlayer.start(); 
   mCurrentState = STATE_PLAYING; 
  } 
  mTargetState = STATE_PLAYING; 
 } 
 public void pause() { 
  if (isInPlaybackState()) { 
   if (mMediaPlayer.isPlaying()) { 
    mMediaPlayer.pause(); 
    mCurrentState = STATE_PAUSED; 
   } 
  } 
  mTargetState = STATE_PAUSED; 
 } 
 // cache duration as mDuration for faster access 
 public int getDuration() { 
  if (isInPlaybackState()) { 
   if (mDuration > 0) { 
    return mDuration; 
   } 
   mDuration = mMediaPlayer.getDuration(); 
   return mDuration; 
  } 
  mDuration = -1; 
  return mDuration; 
 } 
 public int getCurrentPosition() { 
  if (isInPlaybackState()) { 
   return mMediaPlayer.getCurrentPosition(); 
  } 
  return 0; 
 } 
 public void seekTo(int msec) { 
  if (isInPlaybackState()) { 
   mMediaPlayer.seekTo(msec); 
   mSeekWhenPrepared = 0; 
  } else { 
   mSeekWhenPrepared = msec; 
  } 
 }  
 public boolean isPlaying() { 
  return isInPlaybackState() && mMediaPlayer.isPlaying(); 
 } 
 public int getBufferPercentage() { 
  if (mMediaPlayer != null) { 
   return mCurrentBufferPercentage; 
  } 
  return 0; 
 } 
 private boolean isInPlaybackState() { 
  return (mMediaPlayer != null && 
    mCurrentState != STATE_ERROR && 
    mCurrentState != STATE_IDLE && 
    mCurrentState != STATE_PREPARING); 
 } 
 public boolean canPause() { 
  return mCanPause; 
 } 
 public boolean canSeekBackward() { 
  return mCanSeekBack; 
 } 
 public boolean canSeekForward() { 
  return mCanSeekForward; 
 } 
} 

       以上就是对Android VideoView 类的详细介绍,后续继续补充相关知识,谢谢大家对本站的支持!

您可能感兴趣的文章:android视频播放简单实现示例(VideoView&MediaPlayer)Android videoview抢占焦点的处理方法android之视频播放系统VideoView和自定义VideoView控件的应用Android编程实现VideoView循环播放功能的方法Android多媒体之VideoView视频播放器Android使用VideoView播放本地视频和网络视频的方法详解Android App中使用VideoView来实现视频播放的方法Android自定义播放器控件VideoViewandroid使用videoview播放视频android多媒体类VideoView使用方法详解


免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

Android VideoView类实例讲解

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

Android VideoView类实例讲解

本节使用系统的示例类VideoView继续SurfaceView类相关内容的讲解,以让大家能更深入理解Android系统中图形绘制基础类的实现原理。也许你会发现无法改变VideoView类的控制方面,我们可以通过重构Vide
2022-06-06

JavaScriptClass类实例讲解

ES6提供了更接近面向对象(注意:javascript本质上是基于对象的语言)语言的写法,引入了Class(类)这个概念,作为对象的模板,下面这篇文章主要给大家介绍了关于JavaScriptES6中class定义类的相关资料,需要的朋友可以参考下
2022-11-13

android多媒体类VideoView使用方法详解

一、概述VideoView类将视频的显示和控制集于一身,我们可以借助它完成一个简易的视频播放器。VideoView和MediaPlayer也比较相似。二、VideoView的使用方法它主要有以下几种常用方法步骤:1.指定视频文件的路径,2.
2023-05-30

android视频播放简单实现示例(VideoView&MediaPlayer)

如果你看过我的《android音乐播放简单实现(MediaPlayer)》,那么本篇将会毫无压力。首先是主界面的三个按钮和一个播放控件
2023-05-30

TypeScript中class类型实例讲解

众所周知在TypeScript中定义变量需要指定标识符的类型,下面这篇文章主要给大家介绍了关于TypeScript中class类型的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
2022-12-30

Android中SharedPreference使用实例讲解

SharedPreference方面的内容还算是比较简单易懂的,在此还是主要贴上效果与代码,最后也是附上源码。 首先是输入账号admin,密码123,选择记住密码登陆。 登陆后就直接跳转页面。 随后再次打开app可以发现
2022-06-06

关于random类与scanner类的实例讲解

生成指定范围内的随机数Math.random() 生成随机数,随机数在0到1之间,类型是 double。代码示例:public class randCase { public static void main(String[] args) {
关于random类与scanner类的实例讲解
2021-08-21

Java异常 Exception类及其子类(实例讲解)

C语言时用if...else...来控制异常,Java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象。Java异常处理通过5个关键字try、catch、finally、throw、thr
2023-05-30

Android手势操作简单实例讲解

上一篇介绍的onTouch提供的事件还是相对较简单,如果需要处理一些复杂的手势,用这个接口就会很麻烦,因为我们要根据用户触摸的轨迹去判断是什么手势。幸好Android SDK给我们提供了GestureDetector类,通过这个类我们可以识
2022-06-06

实例讲解Android中SQLiteDatabase使用方法

SQLite数据库是android系统内嵌的数据库,小巧强大,能够满足大多数SQL语句的处理工作,而SQLite数据库仅仅是个文件而已。虽然SQLite的有点很多,但并不是如同PC端的mysql般强大,而且android系统中不允许通过JD
2022-06-06

Android 对话框 Dialog使用实例讲解

对话框 Dialog 什么是对话框 对话框是在当前的页面之上弹出的小窗口, 用于显示一些重要的提示信息, 提示用户的输入,确认信息,或显示某种状态.如 : 显示进度条对话框, 退出提示. 对话框的特点: 1, 当前界面弹出的小窗口. 2,
2022-06-06

Android计时器chronometer使用实例讲解

在Android中,可以使用计时器来实现对时间的监测,这个类所实现的功能有开始计时,停止计时,重新计时,设置计 时模式,下面列出计时器方法的原型: long getBase();//返回基地的时间,由setBase(long)设置的 Str
2022-06-06

Android自定义控件LinearLayout实例讲解

很多时候Android常用的控件不能满足我们的需求,那么我们就需要自定义一个控件了。今天做了一个自定义控件的实例,来分享下。 首先定义一个layout实现按钮内部布局: <
2022-06-06

Android判断用户的网络类型实例讲解(2/3/4G、wifi)

很多时候需要先判断当前用户的网络,才会继续之后的一些处理逻辑。但网络类型获取这一块,我用我自己的的手机调试时遇到一些问题,这里记录一下。一加手机一代,移动4G 网络,得到的subtype类型值为17,我查过Android 5.1的源码,它最
2022-06-06

Java中类加载机制的实例讲解

这篇文章主要介绍“Java中类加载机制的实例讲解”,在日常操作中,相信很多人在Java中类加载机制的实例讲解问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Java中类加载机制的实例讲解”的疑惑有所帮助!接下来
2023-06-20

通过Class类获取对象(实例讲解)

通过Class对象获取对象的方式是通过class.newInstance()方式获取,通过调用默认构造参数实例化一个对象。/** * Created by hunt on 2017/6/27. * 测试的实体类 * @Data 编译后会自动
2023-05-31

编程热搜

  • Android:VolumeShaper
    VolumeShaper(支持版本改一下,minsdkversion:26,android8.0(api26)进一步学习对声音的编辑,可以让音频的声音有变化的播放 VolumeShaper.Configuration的三个参数 durati
    Android:VolumeShaper
  • Android崩溃异常捕获方法
    开发中最让人头疼的是应用突然爆炸,然后跳回到桌面。而且我们常常不知道这种状况会何时出现,在应用调试阶段还好,还可以通过调试工具的日志查看错误出现在哪里。但平时使用的时候给你闹崩溃,那你就欲哭无泪了。 那么今天主要讲一下如何去捕捉系统出现的U
    Android崩溃异常捕获方法
  • android开发教程之获取power_profile.xml文件的方法(android运行时能耗值)
    系统的设置–>电池–>使用情况中,统计的能耗的使用情况也是以power_profile.xml的value作为基础参数的1、我的手机中power_profile.xml的内容: HTC t328w代码如下:
    android开发教程之获取power_profile.xml文件的方法(android运行时能耗值)
  • Android SQLite数据库基本操作方法
    程序的最主要的功能在于对数据进行操作,通过对数据进行操作来实现某个功能。而数据库就是很重要的一个方面的,Android中内置了小巧轻便,功能却很强的一个数据库–SQLite数据库。那么就来看一下在Android程序中怎么去操作SQLite数
    Android SQLite数据库基本操作方法
  • ubuntu21.04怎么创建桌面快捷图标?ubuntu软件放到桌面的技巧
    工作的时候为了方便直接打开编辑文件,一些常用的软件或者文件我们会放在桌面,但是在ubuntu20.04下直接直接拖拽文件到桌面根本没有效果,在进入桌面后发现软件列表中的软件只能收藏到面板,无法复制到桌面使用,不知道为什么会这样,似乎并不是很
    ubuntu21.04怎么创建桌面快捷图标?ubuntu软件放到桌面的技巧
  • android获取当前手机号示例程序
    代码如下: public String getLocalNumber() { TelephonyManager tManager =
    android获取当前手机号示例程序
  • Android音视频开发(三)TextureView
    简介 TextureView与SurfaceView类似,可用于显示视频或OpenGL场景。 与SurfaceView的区别 SurfaceView不能使用变换和缩放等操作,不能叠加(Overlay)两个SurfaceView。 Textu
    Android音视频开发(三)TextureView
  • android获取屏幕高度和宽度的实现方法
    本文实例讲述了android获取屏幕高度和宽度的实现方法。分享给大家供大家参考。具体分析如下: 我们需要获取Android手机或Pad的屏幕的物理尺寸,以便于界面的设计或是其他功能的实现。下面就介绍讲一讲如何获取屏幕的物理尺寸 下面的代码即
    android获取屏幕高度和宽度的实现方法
  • Android自定义popupwindow实例代码
    先来看看效果图:一、布局
  • Android第一次实验
    一、实验原理 1.1实验目标 编程实现用户名与密码的存储与调用。 1.2实验要求 设计用户登录界面、登录成功界面、用户注册界面,用户注册时,将其用户名、密码保存到SharedPreference中,登录时输入用户名、密码,读取SharedP
    Android第一次实验

目录