1.设置状态栏透明
if ( Build . VERSION. SDK_INT >= 21 ) {
View decorView = getWindow ( ) . getDecorView ( ) ;
decorView. setSystemUiVisibility ( View . SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View . SYSTEM_UI_FLAG_LAYOUT_STABLE) ;
getWindow ( ) . setStatusBarColor ( Color . TRANSPARENT) ;
int ui = decorView. getSystemUiVisibility ( ) ;
ui |= View . SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
decorView. setSystemUiVisibility ( ui) ;
}
if ( Build . VERSION. SDK_INT >= 21 ) {
View decorView = getWindow ( ) . getDecorView ( ) ;
decorView. setSystemUiVisibility ( View . SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View . SYSTEM_UI_FLAG_LAYOUT_STABLE) ;
getWindow ( ) . setStatusBarColor ( Color . BLACK) ;
int ui = decorView. getSystemUiVisibility ( ) ;
ui |= View . SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
decorView. setSystemUiVisibility ( ui) ;
}
2.Dialog样式一(是/否)
private void showNormalDialog ( final String xid) {
final AlertDialog. Builder normalDialog =
new AlertDialog. Builder ( alldata. this ) ;
normalDialog. setTitle ( "删除" ) ;
normalDialog. setMessage ( "确认是否删除?" ) ;
normalDialog. setPositiveButton ( "确定" ,
new DialogInterface. OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog, int which) {
del ( xid) ;
}
} ) ;
normalDialog. setNegativeButton ( "关闭" ,
new DialogInterface. OnClickListener ( ) {
@Override
public void onClick ( DialogInterface dialog, int which) {
Toast . makeText ( alldata. this , "关闭" , Toast . LENGTH_SHORT) . show ( ) ;
}
} ) ;
normalDialog. show ( ) ;
}
2.1Dialog样式二(新手引导)
private void newYingdao ( ) {
LayoutInflater inflater = LayoutInflater . from ( courseInfo. this ) ;
View view = inflater. inflate ( R . layout. tips1, null ) ;
AlertDialog. Builder builder= new AlertDialog. Builder ( courseInfo. this , R . style. TransparentDialog) ;
builder. setView ( view) ;
final AlertDialog dialog= builder. create ( ) ;
final Button b= view. findViewById ( R . id. button4) ;
final LinearLayout tips= view. findViewById ( R . id. tips) ;
tips. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
dialog. dismiss ( ) ;
}
} ) ;
b. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
dialog. dismiss ( ) ;
}
} ) ;
dialog. show ( ) ;
}
< style name= "TransparentDialog" parent= "Theme.AppCompat.Dialog" >
< item name= "android:windowBackground" > @color / transparent< / item>
< / style>
2.2 Diaglog点击其他地方不销毁
final AlertDialog dialog= builder. create ( ) ;
dialog. setCancelable ( false ) ;
3.从下方弹出页面
private void dialogs2 ( ) {
LayoutInflater inflater = LayoutInflater . from ( getApplication ( ) ) ;
View view = inflater. inflate ( R . layout. home_ulist, null ) ;
AlertDialog. Builder builder= new AlertDialog. Builder ( home. this ) ;
builder. setView ( view) ;
final AlertDialog dialog= builder. create ( ) ;
Window window = dialog. getWindow ( ) ;
final TextView bianji= view. findViewById ( R . id. bianji) ;
users ( bianji) ;
window. setGravity ( Gravity . BOTTOM) ;
window. setWindowAnimations ( R . style. SelectUpload2) ;
dialog. show ( ) ;
WindowManager. LayoutParams params =
dialog. getWindow ( ) . getAttributes ( ) ;
WindowManager wm = ( WindowManager ) home. this . getSystemService ( Context . WINDOW_SERVICE) ;
params. width = WindowManager. LayoutParams . MATCH_PARENT;
dialog. getWindow ( ) . setBackgroundDrawableResource ( R . color. white) ;
dialog. getWindow ( ) . setAttributes ( params) ;
}
< style name= "SelectUpload2" >
< item name= "android:windowEnterAnimation" > @anim / dialog_enter< / item>
< item name= "android:windowExitAnimation" > @anim / dialog_exit< / item>
< / style>
4.封装的PrefStore
import android. content. Context ;
import android. content. SharedPreferences ;
import android. content. SharedPreferences. Editor ;
public class PrefStore {
private static final String STORE_NAME = "Settings" ;
private static Context mContext = null ;
private static PrefStore instance = null ;
public static PrefStore getInstance ( Context context) {
if ( instance == null ) {
instance = new PrefStore ( context) ;
}
return instance;
}
public PrefStore ( Context context) {
mContext = context. getApplicationContext ( ) ;
}
public boolean savePref ( String key, String value) {
if ( mContext != null ) {
SharedPreferences pref = mContext. getSharedPreferences ( STORE_NAME,
Context . MODE_PRIVATE) ;
Editor editor = pref. edit ( ) ;
editor. putString ( key, value) ;
editor. commit ( ) ;
return true ;
} else {
return false ;
}
}
public String getPref ( String key, String defValue) {
if ( mContext != null ) {
SharedPreferences pref = mContext. getSharedPreferences ( STORE_NAME,
Context . MODE_PRIVATE) ;
return pref. getString ( key, defValue) ;
} else {
return null ;
}
}
public boolean removePref ( String key) {
if ( mContext != null ) {
SharedPreferences pref = mContext. getSharedPreferences ( STORE_NAME,
Context . MODE_PRIVATE) ;
Editor editor = pref. edit ( ) ;
editor. remove ( key) ;
editor. commit ( ) ;
return true ;
} else {
return false ;
}
}
public boolean clearPref ( ) {
if ( mContext != null ) {
SharedPreferences pref = mContext. getSharedPreferences ( STORE_NAME,
Context . MODE_MULTI_PROCESS) ;
Editor editor = pref. edit ( ) ;
editor. clear ( ) ;
editor. commit ( ) ;
return true ;
} else {
return false ;
}
}
}
4.1 封装的PrefStore使用
PrefStore pref = PrefStore . getInstance ( Login . this ) ;
pref. savePref ( "Phone" , USERS. getPhone ( ) ) ;
pref. getPref ( "Uid" , "1" )
5.封装的PicassoUtils
PicassoUtils . loadImageViewCrop2 ( ChangeHeadImage . this , localhost_path+ result, imageView) ;
6.封装的OkHttpUtils
import android. graphics. Bitmap ;
import android. graphics. BitmapFactory ;
import android. os. Handler ;
import android. os. Looper ;
import android. util. Log ;
import org. json. JSONException ;
import org. json. JSONObject ;
import java. io. IOException ;
import java. util. Map ;
import okhttp3. Call ;
import okhttp3. Callback ;
import okhttp3. FormBody ;
import okhttp3. MediaType ;
import okhttp3. OkHttpClient ;
import okhttp3. Request ;
import okhttp3. RequestBody ;
import okhttp3. Response ;
public class OkHttpUtils {
private static final String TAG = OkHttpUtils . class . getSimpleName ( ) ;
private OkHttpClient client;
private volatile static OkHttpUtils okHttpUtils;
private static final MediaType JSON = MediaType . parse ( "application/json;charset=utf-8" ) ;
private static final MediaType STRING = MediaType . parse ( "text/x-markdown;charset=utf-8" ) ;
private Handler handler;
private OkHttpUtils ( ) {
client = new OkHttpClient ( ) ;
handler = new Handler ( Looper . getMainLooper ( ) ) ;
}
public static OkHttpUtils getInstance ( ) {
OkHttpUtils okUtils = new OkHttpUtils ( ) ;
return okUtils;
}
private void onsuccessJsonStringMethod ( final String jsonValue, final FuncJsonString callBack) {
handler. post ( new Runnable ( ) {
@Override
public void run ( ) {
if ( callBack != null ) {
try {
callBack. onResponse ( jsonValue) ;
} catch ( Exception e) {
}
}
}
} ) ;
}
private void onsuccessJsonObjectMethod ( final String jsonValue, final FuncJsonObject callBack) {
handler. post ( new Runnable ( ) {
@Override
public void run ( ) {
if ( callBack != null ) {
try {
callBack. onResponse ( new JSONObject ( jsonValue) ) ;
} catch ( JSONException e) {
e. printStackTrace ( ) ;
}
}
}
} ) ;
}
private void onsuccessJsonByteMethod ( final byte [ ] data, final FuncJsonObjectByte callBack) {
handler. post ( new Runnable ( ) {
@Override
public void run ( ) {
if ( callBack != null ) {
callBack. onResponse ( data) ;
}
}
} ) ;
}
public String syncGetByURL ( String url) {
Request request = new Request. Builder ( ) . url ( url) . build ( ) ;
Response response = null ;
try {
response = client. newCall ( request) . execute ( ) ;
if ( response. isSuccessful ( ) ) {
return response. body ( ) . string ( ) ;
}
} catch ( Exception e) {
}
return null ;
}
public void syncJsonStringByURL ( String url, final FuncJsonString callback) {
final Request request = new Request. Builder ( ) . url ( url) . build ( ) ;
client. newCall ( request) . enqueue ( new Callback ( ) {
@Override
public void onFailure ( Call call, IOException e) {
Log . i ( TAG, "解析失败" ) ;
}
@Override
public void onResponse ( Call call, Response response) throws IOException {
if ( response != null && response. isSuccessful ( ) ) {
onsuccessJsonStringMethod ( response. body ( ) . string ( ) , callback) ;
}
}
} ) ;
}
public void syscJsonObjectByURL ( String url, final FuncJsonObject callback) {
final Request request = new Request. Builder ( ) . url ( url) . build ( ) ;
client. newCall ( request) . enqueue ( new Callback ( ) {
@Override
public void onFailure ( Call call, IOException e) {
Log . i ( TAG, "解析失败" ) ;
}
@Override
public void onResponse ( Call call, Response response) throws IOException {
if ( response != null && response. isSuccessful ( ) ) {
onsuccessJsonObjectMethod ( response. body ( ) . string ( ) , callback) ;
}
}
} ) ;
}
public void syscGetByteByURL ( String url, final FuncJsonObjectByte callback) {
final Request request = new Request. Builder ( ) . url ( url) . build ( ) ;
client. newCall ( request) . enqueue ( new Callback ( ) {
@Override
public void onFailure ( Call call, IOException e) {
Log . i ( TAG, "解析失败" ) ;
}
@Override
public void onResponse ( Call call, Response response) throws IOException {
if ( response != null && response. isSuccessful ( ) ) {
onsuccessJsonByteMethod ( response. body ( ) . bytes ( ) , callback) ;
}
}
} ) ;
}
public void syscDownloadImageByURL ( String url, final FuncJsonObjectBitmap callback) {
final Request request = new Request. Builder ( ) . url ( url) . build ( ) ;
client. newCall ( request) . enqueue ( new Callback ( ) {
@Override
public void onFailure ( Call call, IOException e) {
Log . i ( TAG, "解析失败" ) ;
}
@Override
public void onResponse ( Call call, Response response) throws IOException {
if ( response != null && response. isSuccessful ( ) ) {
byte [ ] data = response. body ( ) . bytes ( ) ;
Bitmap bitmap = new PicassoUtils. CropImageView ( ) . transform ( BitmapFactory . decodeByteArray ( data, 0 , data. length) ) ;
callback. onResponse ( bitmap) ;
}
}
} ) ;
}
public void sendDatafForClicent ( String url, Map < String , String > params, final FuncJsonObject callback) {
FormBody. Builder from = new FormBody. Builder ( ) ;
if ( params != null && ! params. isEmpty ( ) ) {
for ( Map. Entry < String , String > entry : params. entrySet ( ) ) {
from. add ( entry. getKey ( ) , entry. getValue ( ) ) ;
}
}
RequestBody body = from. build ( ) ;
Request request = new Request. Builder ( ) . url ( url) . post ( body) . build ( ) ;
client. newCall ( request) . enqueue ( new Callback ( ) {
@Override
public void onFailure ( Call call, IOException e) {
Log . i ( TAG, "解析失败" ) ;
}
@Override
public void onResponse ( Call call, Response response) throws IOException {
if ( response != null && response. isSuccessful ( ) ) {
onsuccessJsonObjectMethod ( response. body ( ) . string ( ) , callback) ;
}
}
} ) ;
}
public void sendDatafForClicent2 ( String url, Map < String , String > params, final FuncJsonString callback) {
FormBody. Builder from = new FormBody. Builder ( ) ;
if ( params != null && ! params. isEmpty ( ) ) {
for ( Map. Entry < String , String > entry : params. entrySet ( ) ) {
from. add ( entry. getKey ( ) , entry. getValue ( ) ) ;
}
}
RequestBody body = from. build ( ) ;
Request request = new Request. Builder ( ) . url ( url) . post ( body) . build ( ) ;
client. newCall ( request) . enqueue ( new Callback ( ) {
@Override
public void onFailure ( Call call, IOException e) {
Log . i ( TAG, "解析失败" ) ;
}
@Override
public void onResponse ( Call call, Response response) throws IOException {
if ( response != null && response. isSuccessful ( ) ) {
onsuccessJsonStringMethod ( response. body ( ) . string ( ) , callback) ;
}
}
} ) ;
}
public void sendDatafForClicent3 ( String url, Map < String , String > params, final FuncJsonString callback) {
FormBody. Builder from = new FormBody. Builder ( ) ;
if ( params != null && ! params. isEmpty ( ) ) {
for ( Map. Entry < String , String > entry : params. entrySet ( ) ) {
from. add ( entry. getKey ( ) , entry. getValue ( ) ) ;
}
}
RequestBody body = from. build ( ) ;
Request request = new Request. Builder ( ) . url ( url) . post ( body) . build ( ) ;
client. newCall ( request) . enqueue ( new Callback ( ) {
@Override
public void onFailure ( Call call, IOException e) {
Log . i ( TAG, "解析失败" ) ;
}
@Override
public void onResponse ( Call call, Response response) throws IOException {
if ( response != null && response. isSuccessful ( ) ) {
onsuccessJsonStringMethod ( response. body ( ) . string ( ) , callback) ;
}
}
} ) ;
}
public void sendPostNoParams ( String url, final FuncJsonString callback) {
FormBody. Builder from = new FormBody. Builder ( ) ;
RequestBody body = from. build ( ) ;
Request request = new Request. Builder ( ) . url ( url) . post ( body) . build ( ) ;
client. newCall ( request) . enqueue ( new Callback ( ) {
@Override
public void onFailure ( Call call, IOException e) {
Log . i ( TAG, "解析失败" ) ;
}
@Override
public void onResponse ( Call call, Response response) throws IOException {
if ( response != null && response. isSuccessful ( ) ) {
onsuccessJsonStringMethod ( response. body ( ) . string ( ) , callback) ;
}
}
} ) ;
}
public interface FuncJsonString {
void onResponse ( String result) ;
}
public interface FuncJsonObject {
void onResponse ( JSONObject jsonObject) ;
}
interface FuncJsonObjectByte {
void onResponse ( byte [ ] result) ;
}
interface FuncJsonObjectBitmap {
void onResponse ( Bitmap bitmap) ;
}
}
6.1 封装的OkHttpUtils发送 Post请求
private void Post ( ) {
blogInfos. clear ( ) ;
Map < String , String > hs= new HashMap < > ( ) ;
hs. put ( "uname" , String . valueOf ( "admin-all" ) ) ;
OkHttpUtils okHttp = OkHttpUtils . getInstance ( ) ;
okHttp. sendDatafForClicent2 ( getAllXiangList, hs, new OkHttpUtils. FuncJsonString ( ) {
@Override
public void onResponse ( String result) {
JsonParser parser = new JsonParser ( ) ;
JsonArray jsonArray = parser. parse ( result) . getAsJsonArray ( ) ;
Gson gson = new Gson ( ) ;
PrefStore pref = PrefStore . getInstance ( mydata. this ) ;
for ( JsonElement Title : jsonArray) {
Xiangzi g= gson. fromJson ( Title , Xiangzi . class ) ;
if ( g. getUserid ( ) == Integer . valueOf ( pref. getPref ( "Uid" , "1" ) ) ) {
blogInfos. add ( g) ;
}
}
initListView ( ) ;
}
} ) ;
}
7.复用的适配器
import android. content. Context ;
import android. view. LayoutInflater ;
import android. view. View ;
import android. view. ViewGroup ;
import android. view. animation. AlphaAnimation ;
import android. view. animation. Animation ;
import android. view. animation. LinearInterpolator ;
import android. widget. BaseAdapter ;
import android. widget. ImageView ;
import android. widget. LinearLayout ;
import android. widget. TextView ;
import com. example. sushe. R ;
import com. example. sushe. model. Xiangzi ;
import com. example. sushe. url. url ;
import com. example. sushe. utils. OkHttpUtils ;
import com. example. sushe. utils. PrefStore ;
import java. util. HashMap ;
import java. util. List ;
import java. util. Map ;
public class ListAdapter extends BaseAdapter {
private List < Xiangzi > mList;
private Context mContext;
public ListAdapter ( Context mContext, List < Xiangzi > mList) {
this . mContext= mContext;
this . mList= mList;
}
@Override
public int getCount ( ) {
return mList!= null ? mList. size ( ) : 0 ;
}
@Override
public Object getItem ( int position) {
return mList!= null ? mList. get ( position) : null ;
}
@Override
public long getItemId ( int position) {
return position;
}
@Override
public View getView ( final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null ;
if ( convertView == null ) {
convertView = LayoutInflater . from ( mContext) . inflate ( R . layout. bloglist_item, parent, false ) ;
holder = new ViewHolder ( convertView) ;
convertView. setTag ( holder) ;
} else {
holder = ( ViewHolder ) convertView. getTag ( ) ;
}
final ViewHolder finalHolder = holder;
return convertView;
}
private class ViewHolder {
LinearLayout root;
public ViewHolder ( View view) {
if ( view== null )
return ;
root= view. findViewById ( R . id. root) ;
}
}
}
8.抖音的简单实现
implementation 'com.shuyu:GSYVideoPlayer:5.0.2'
import android. annotation. TargetApi ;
import android. os. Build ;
import android. os. Bundle ;
import android. support. v7. app. AppCompatActivity ;
import android. support. v7. widget. OrientationHelper ;
import android. support. v7. widget. RecyclerView ;
import android. util. Log ;
import android. view. LayoutInflater ;
import android. view. View ;
import android. view. ViewGroup ;
import android. widget. ImageView ;
import android. widget. LinearLayout ;
import android. widget. TextView ;
import android. widget. Toast ;
import com. example. sushe. R ;
import com. shuyu. gsyvideoplayer. GSYVideoManager ;
import com. shuyu. gsyvideoplayer. utils. OrientationUtils ;
import com. shuyu. gsyvideoplayer. video. StandardGSYVideoPlayer ;
import java. util. ArrayList ;
import static com. example. sushe. url. url. localhost_path ;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "--------------->" ;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
MyLayoutManager myLayoutManager;
private OrientationUtils orientationUtils;
private ArrayList < String > ls = new ArrayList < String > ( ) ;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_dys) ;
ls. add ( localhost_path+ "/vedio/mm1.mp4" ) ;
ls. add ( localhost_path+ "/vedio/mm5.mp4" ) ;
ls. add ( localhost_path+ "/vedio/mm3.mp4" ) ;
ls. add ( localhost_path+ "/vedio/mm1.mp4" ) ;
ls. add ( localhost_path+ "/vedio/mm5.mp4" ) ;
ls. add ( localhost_path+ "/vedio/mm3.mp4" ) ;
initView ( ) ;
initListener ( ) ;
}
private void initView ( ) {
mRecyclerView = findViewById ( R . id. recycler) ;
myLayoutManager = new MyLayoutManager ( this , OrientationHelper . VERTICAL, false ) ;
mAdapter = new MyAdapter ( ) ;
mRecyclerView. setLayoutManager ( myLayoutManager) ;
mRecyclerView. setAdapter ( mAdapter) ;
}
private void initListener ( ) {
myLayoutManager. setOnViewPagerListener ( new OnViewPagerListener ( ) {
@Override
public void onInitComplete ( ) {
}
@Override
public void onPageRelease ( boolean isNext, int position) {
Log . e ( TAG, "释放位置:" + position + " 下一页:" + isNext) ;
Toast . makeText ( MainActivity . this , "23" , Toast . LENGTH_SHORT) . show ( ) ;
releaseVideo ( position) ;
}
@Override
public void onPageSelected ( int position, boolean isNext) {
Log . e ( TAG, "释放位置:" + position + " 下一页:" + isNext) ;
Toast . makeText ( MainActivity . this , "23" , Toast . LENGTH_SHORT) . show ( ) ;
playVideo ( position) ;
}
} ) ;
}
class MyAdapter extends RecyclerView. Adapter < MyAdapter. ViewHolder > {
public MyAdapter ( ) {
}
@Override
public ViewHolder onCreateViewHolder ( ViewGroup parent, int viewType) {
View view = LayoutInflater . from ( parent. getContext ( ) ) . inflate ( R . layout. item_view_pager, parent, false ) ;
return new ViewHolder ( view) ;
}
@Override
public void onBindViewHolder ( ViewHolder holder, int position) {
holder. t. setVisibility ( View . GONE) ;
ImageView imageView = new ImageView ( MainActivity . this ) ;
imageView. setScaleType ( ImageView. ScaleType . CENTER_CROP) ;
holder. videoPlayer. setThumbImageView ( imageView) ;
holder. videoPlayer. getTitleTextView ( ) . setVisibility ( View . VISIBLE) ;
holder. videoPlayer. getBackButton ( ) . setVisibility ( View . VISIBLE) ;
orientationUtils = new OrientationUtils ( MainActivity . this , holder. videoPlayer) ;
holder. videoPlayer. getFullscreenButton ( ) . setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
orientationUtils. resolveByClick ( ) ;
}
} ) ;
holder. videoPlayer. setIsTouchWiget ( true ) ;
holder. videoPlayer. getBackButton ( ) . setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
onBackPressed ( ) ;
}
} ) ;
holder. videoPlayer. startPlayLogic ( ) ;
}
@Override
public int getItemCount ( ) {
return 15 ;
}
public class ViewHolder extends RecyclerView. ViewHolder {
StandardGSYVideoPlayer videoPlayer;
LinearLayout z1;
TextView t;
public ViewHolder ( View itemView) {
super ( itemView) ;
videoPlayer = itemView. findViewById ( R . id. video_view) ;
z1= itemView. findViewById ( R . id. z1) ;
t= itemView. findViewById ( R . id. textView16) ;
}
}
}
private void releaseVideo ( int index) {
View itemView = mRecyclerView. getChildAt ( index) ;
final StandardGSYVideoPlayer videoPlayer = itemView. findViewById ( R . id. video_view) ;
videoPlayer. onVideoPause ( ) ;
}
@TargetApi ( Build . VERSION_CODES. JELLY_BEAN_MR1)
private void playVideo ( int position) {
View itemView = mRecyclerView. getChildAt ( 0 ) ;
final StandardGSYVideoPlayer videoPlayer = itemView. findViewById ( R . id. video_view) ;
final TextView z1= itemView. findViewById ( R . id. textView16) ;
videoPlayer. startPlayLogic ( ) ;
}
@Override
protected void onDestroy ( ) {
super . onDestroy ( ) ;
GSYVideoManager . releaseAllVideos ( ) ;
if ( orientationUtils != null ) {
orientationUtils. releaseListener ( ) ;
}
}
}
import android. content. Context ;
import android. support. annotation. NonNull ;
import android. support. v7. widget. LinearLayoutManager ;
import android. support. v7. widget. PagerSnapHelper ;
import android. support. v7. widget. RecyclerView ;
import android. view. View ;
public class MyLayoutManager extends LinearLayoutManager implements RecyclerView. OnChildAttachStateChangeListener {
private int mDrift;
private PagerSnapHelper mPagerSnapHelper;
private OnViewPagerListener mOnViewPagerListener;
public MyLayoutManager ( Context context) {
super ( context) ;
}
public MyLayoutManager ( Context context, int orientation, boolean reverseLayout) {
super ( context, orientation, reverseLayout) ;
mPagerSnapHelper = new PagerSnapHelper ( ) ;
}
@Override
public void onAttachedToWindow ( RecyclerView view) {
view. addOnChildAttachStateChangeListener ( this ) ;
mPagerSnapHelper. attachToRecyclerView ( view) ;
super . onAttachedToWindow ( view) ;
}
@Override
public void onChildViewAttachedToWindow ( @NonNull View view) {
if ( mDrift > 0 ) {
if ( mOnViewPagerListener != null ) {
mOnViewPagerListener. onPageSelected ( getPosition ( view) , true ) ;
}
} else {
if ( mOnViewPagerListener != null ) {
mOnViewPagerListener. onPageSelected ( getPosition ( view) , false ) ;
}
}
}
public void setOnViewPagerListener ( OnViewPagerListener mOnViewPagerListener) {
this . mOnViewPagerListener = mOnViewPagerListener;
}
@Override
public void onScrollStateChanged ( int state) {
switch ( state) {
case RecyclerView . SCROLL_STATE_IDLE:
View view = mPagerSnapHelper. findSnapView ( this ) ;
int position = getPosition ( view) ;
if ( mOnViewPagerListener != null ) {
mOnViewPagerListener. onPageSelected ( position, position == getItemCount ( ) - 1 ) ;
}
break ;
default :
}
super . onScrollStateChanged ( state) ;
}
@Override
public void onChildViewDetachedFromWindow ( @NonNull View view) {
if ( mDrift >= 0 ) {
if ( mOnViewPagerListener != null ) {
}
} else {
if ( mOnViewPagerListener != null ) {
}
}
}
@Override
public int scrollVerticallyBy ( int dy, RecyclerView. Recycler recycler, RecyclerView. State state) {
this . mDrift = dy;
return super . scrollVerticallyBy ( dy, recycler, state) ;
}
@Override
public boolean canScrollVertically ( ) {
return true ;
}
}
public interface OnViewPagerListener {
void onInitComplete ( ) ;
void onPageRelease ( boolean isNext, int position) ;
void onPageSelected ( int position, boolean isBottom) ;
}
< ? xml version= "1.0" encoding= "utf-8" ? >
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: app= "http://schemas.android.com/apk/res-auto"
android: id= "@+id/root_view"
android: layout_width= "match_parent"
android: orientation= "vertical"
android: layout_height= "match_parent" >
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "0dp"
android: layout_weight= "1"
android: orientation= "vertical" >
< com. shuyu. gsyvideoplayer. video. StandardGSYVideoPlayer
android: id= "@+id/video_view"
android: layout_width= "match_parent"
android: layout_height= "match_parent" >
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: layout_marginTop= "45dp"
android: gravity= "center"
android: orientation= "horizontal" >
< TextView
android: id= "@+id/textView2"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: gravity= "center"
android: layout_marginRight= "10dp"
android: textSize= "19dp"
android: textStyle= "bold"
android: text= "学习"
android: textColor= "#FBF8F8" / >
< TextView
android: id= "@+id/textView50"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: gravity= "center"
android: layout_marginLeft= "10dp"
android: textSize= "19dp"
android: text= "娱乐"
android: textStyle= "bold"
android: textColor= "#FBF8F8" / >
< / LinearLayout >
< / com. shuyu. gsyvideoplayer. video. StandardGSYVideoPlayer>
< LinearLayout
android: id= "@+id/z1"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: layout_gravity= "right"
android: layout_marginTop= "-320dp"
android: layout_marginRight= "15dp"
android: gravity= "right"
android: orientation= "vertical" >
< LinearLayout
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: gravity= "left"
android: orientation= "vertical" >
< ImageView
android: id= "@+id/imageView7"
android: layout_width= "45dp"
android: layout_height= "30dp" / >
< TextView
android: id= "@+id/textView16"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: layout_marginTop= "3dp"
android: layout_marginLeft= "-5dp"
android: gravity= "center"
android: text= ""
android: textColor= "#ffffff"
android: textSize= "13.5dp" / >
< ImageView
android: id= "@+id/imageView8"
android: layout_width= "37dp"
android: layout_height= "37dp"
android: layout_marginTop= "15dp"
android: background= "@drawable/pls" / >
< TextView
android: id= "@+id/textView17"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: layout_marginTop= "3dp"
android: layout_marginLeft= "-5dp"
android: gravity= "center"
android: text= ""
android: textColor= "#ffffff"
android: textSize= "13.5dp" / >
< ImageView
android: id= "@+id/imageView10"
android: layout_width= "37dp"
android: layout_height= "37dp"
android: layout_marginTop= "15dp"
android: background= "@drawable/zhuanfa" / >
< TextView
android: id= "@+id/textView42"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: layout_marginTop= "3dp"
android: layout_marginLeft= "-3dp"
android: gravity= "center"
android: text= "0"
android: textColor= "#ffffff"
android: textSize= "13.5dp" / >
< / LinearLayout >
< / LinearLayout >
< LinearLayout
android: id= "@+id/fs"
android: layout_width= "270dp"
android: layout_height= "100dp"
android: layout_marginLeft= "5dp"
android: layout_marginTop= "-5dp"
android: orientation= "vertical" >
< LinearLayout
android: id= "@+id/xiazai"
android: layout_width= "wrap_content"
android: layout_height= "20dp"
android: orientation= "horizontal"
android: visibility= "gone"
>
< ImageView
android: id= "@+id/imageView14"
android: layout_width= "20dp"
android: layout_height= "20dp"
android: background= "@drawable/xiazai" / >
< TextView
android: id= "@+id/textView68"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: textColor= "#ffffff"
android: textStyle= "bold"
android: text= "点击下载源码解析" / >
< / LinearLayout >
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "35dp"
android: orientation= "horizontal" >
< TextView
android: id= "@+id/textView43"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: text= ""
android: textStyle= "bold"
android: textColor= "#ffffff"
android: textSize= "17.5dp" / >
< TextView
android: id= "@+id/textView44"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: layout_marginLeft= "5dp"
android: text= " 2020/12/18/ 22:54:01"
android: textColor= "#EAE4E4"
android: textSize= "16dp" / >
< / LinearLayout >
< TextView
android: id= "@+id/textView38"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: text= "青春就是用来绽放的"
android: textColor= "#EFEDED"
android: textSize= "16dp" / >
< / LinearLayout >
< / LinearLayout >
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "50dp"
android: background= "#000000"
android: visibility= "gone"
android: orientation= "horizontal" >
< TextView
android: id= "@+id/textView41"
android: layout_width= "wrap_content"
android: layout_height= "wrap_content"
android: layout_marginTop= "15dp"
android: layout_marginBottom= "15dp"
android: text= "留下你的精彩评论吧"
android: textSize= "17dp"
android: layout_marginLeft= "15dp"
android: textColor= "#ECEBEB" / >
< LinearLayout
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: gravity= "right"
android: layout_marginTop= "15dp"
android: orientation= "horizontal" >
< ImageView
android: id= "@+id/imageView9"
android: layout_width= "25dp"
android: layout_height= "25dp"
android: layout_marginRight= "15dp"
android: layout_gravity= "right"
android: layout_marginTop= "15dp"
android: background= "@drawable/xiaolian" / >
< / LinearLayout >
< / LinearLayout >
< / LinearLayout >
< ? xml version= "1.0" encoding= "utf-8" ? >
< com. example. amusic. love. Love_dy xmlns: android= "http://schemas.android.com/apk/res/android"
android: id= "@+id/los1"
android: background= "#000000"
android: layout_width= "match_parent"
android: layout_marginTop= "-2dp"
android: layout_height= "match_parent" >
< android. support. v7. widget. RecyclerView
android: id= "@+id/recycler"
android: layout_width= "match_parent"
android: layout_height= "match_parent" >
< / android. support. v7. widget. RecyclerView>
< VideoView
android: id= "@+id/line_bottom"
android: layout_width= "match_parent"
android: layout_height= "1px"
android: background= "#000000" / >
< / com. example. amusic. love. Love_dy>
9.封装的GsonUtils
import com. google. gson. Gson ;
import com. google. gson. JsonArray ;
import com. google. gson. JsonElement ;
import com. google. gson. JsonParser ;
import java. util. ArrayList ;
import java. util. List ;
public class GsonUtils {
private static Gson gson;
static {
gson = new Gson ( ) ;
}
public static < T > T jsonToBean ( String gsonString, Class < T > cls) {
T t = null ;
if ( gson != null ) {
t = gson. fromJson ( gsonString, cls) ;
}
return t;
}
public static String beanToJson ( Object object) {
String gsonString = null ;
if ( gson != null ) {
gsonString = gson. toJson ( object) ;
}
return gsonString;
}
public static < T > List < T > jsonGsonToList ( String json, Class < T > cls) {
Gson gson = new Gson ( ) ;
List < T > list = new ArrayList < > ( ) ;
JsonArray array = new JsonParser ( ) . parse ( json) . getAsJsonArray ( ) ;
for ( final JsonElement elem : array) {
list. add ( gson. fromJson ( elem, cls) ) ;
}
return list;
}
}
9.1 GsonUtils使用
blog= GsonUtils . jsonGsonToList ( result, blog. class ) ;
10.加载时间
public static String disposeTime ( ) {
Calendar c = Calendar . getInstance ( ) ;
c. add ( Calendar . DAY_OF_MONTH, 0 ) ;
SimpleDateFormat sdf = new SimpleDateFormat ( "yyyy/MM/dd HH:mm:ss" ) ;
String time = sdf. format ( c. getTime ( ) ) ;
return time;
}
11.获取屏幕高度/宽度
public static int getAndroiodScreenHeigh ( WindowManager wm) {
DisplayMetrics dm = new DisplayMetrics ( ) ;
wm. getDefaultDisplay ( ) . getMetrics ( dm) ;
int height = dm. heightPixels;
return height;
}
public static int getAndroiodScreenWidth ( WindowManager wm) {
DisplayMetrics dm = new DisplayMetrics ( ) ;
wm. getDefaultDisplay ( ) . getMetrics ( dm) ;
int width = dm. widthPixels;
return width;
}
12.android简单适配
private void checkState ( ) {
boolean hasAccessibility = AccessibilityUtil . isSettingOpen ( AutoTouchService . class , MainActivity . this ) ;
boolean hasWinPermission = FloatWinPermissionCompat . getInstance ( ) . check ( this ) ;
if ( hasAccessibility) {
if ( hasWinPermission) {
tvStart. setText ( STRING_START) ;
} else {
tvStart. setText ( STRING_ALERT) ;
}
} else {
tvStart. setText ( STRING_ACCESS) ;
}
}
12.1 悬浮窗权限兼容类
import android. annotation. TargetApi ;
import android. app. Activity ;
import android. app. AppOpsManager ;
import android. content. Context ;
import android. content. Intent ;
import android. os. Binder ;
import android. os. Build ;
import android. util. Log ;
import java. lang. reflect. Method ;
public class FloatWinPermissionCompat {
private static final String TAG = FloatWinPermissionCompat . class . getSimpleName ( ) ;
public static FloatWinPermissionCompat getInstance ( ) {
return SingletonHolder . INSTANCE;
}
private static class SingletonHolder {
private static final FloatWinPermissionCompat INSTANCE = new FloatWinPermissionCompat ( ) ;
}
private CompatImpl compat;
private FloatWinPermissionCompat ( ) {
if ( Build . VERSION. SDK_INT < 23 ) {
if ( RomUtils . isMiui ( ) ) {
compat = new MiuiCompatImpl ( ) ;
} else if ( RomUtils . isMeizu ( ) ) {
compat = new MeizuCompatImpl ( ) ;
} else if ( RomUtils . isHuawei ( ) ) {
compat = new HuaweiCompatImpl ( ) ;
} else if ( RomUtils . isQihoo ( ) ) {
compat = new QihooCompatImpl ( ) ;
} else {
compat = new BelowApi23CompatImpl ( ) {
@Override
public boolean isSupported ( ) {
return false ;
}
@Override
public boolean apply ( Context context) {
return false ;
}
} ;
}
} else {
if ( RomUtils . isMeizu ( ) ) {
compat = new MeizuCompatImpl ( ) ;
} else {
compat = new Api23CompatImpl ( ) ;
}
}
}
public boolean check ( Context context) {
return compat. check ( context) ;
}
public boolean isSupported ( ) {
return compat. isSupported ( ) ;
}
@TargetApi ( Build . VERSION_CODES. KITKAT)
public static boolean checkOp ( Context context, int op) {
final int version = Build . VERSION. SDK_INT;
if ( version >= 19 ) {
AppOpsManager manager = ( AppOpsManager ) context. getSystemService ( Context . APP_OPS_SERVICE) ;
try {
Class clazz = AppOpsManager . class ;
Method method = clazz. getDeclaredMethod ( "checkOp" , int . class , int . class , String . class ) ;
return AppOpsManager . MODE_ALLOWED == ( int ) method. invoke ( manager, op, Binder . getCallingUid ( ) , context. getPackageName ( ) ) ;
} catch ( Exception e) {
Log . e ( TAG, Log . getStackTraceString ( e) ) ;
}
} else {
Log . e ( TAG, "Below API 19 cannot invoke!" ) ;
}
return false ;
}
public interface CompatImpl {
boolean check ( Context context) ;
boolean isSupported ( ) ;
boolean apply ( Context context) ;
}
public boolean apply ( Context context) {
if ( ! isSupported ( ) ) {
return false ;
}
forResult = false ;
this . context = context;
return compat. apply ( context) ;
}
public boolean apply ( Activity activity) {
if ( activity == null || ! isSupported ( ) ) {
return false ;
}
this . activity = activity;
this . context = activity. getApplicationContext ( ) ;
forResult = true ;
return compat. apply ( context) ;
}
public static final int REQUEST_CODE_SYSTEM_WINDOW = 1001 ;
private Activity activity;
private Context context;
private boolean forResult = false ;
public void startActivity ( Intent intent) {
try {
if ( intent == null || context == null ) {
return ;
}
if ( ! forResult) {
context. startActivity ( intent) ;
} else {
if ( activity != null ) {
activity. startActivityForResult ( intent, REQUEST_CODE_SYSTEM_WINDOW) ;
}
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
}
12.2 华为悬浮窗权限兼容实现
import android. content. ActivityNotFoundException ;
import android. content. ComponentName ;
import android. content. Context ;
import android. content. Intent ;
import android. util. Log ;
public class HuaweiCompatImpl extends BelowApi23CompatImpl {
private static final String TAG = "HuaweiCompatImpl" ;
@Override
public boolean isSupported ( ) {
return true ;
}
@Override
public boolean apply ( Context context) {
try {
Intent intent = new Intent ( ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
ComponentName comp = new ComponentName ( "com.huawei.systemmanager" , "com.huawei.systemmanager.addviewmonitor.AddViewMonitorActivity" ) ;
intent. setComponent ( comp) ;
if ( RomUtils . getEmuiVersion ( ) == 3.1 ) {
startActivity ( context, intent) ;
} else {
comp = new ComponentName ( "com.huawei.systemmanager" , "com.huawei.notificationmanager.ui.NotificationManagmentActivity" ) ;
intent. setComponent ( comp) ;
startActivity ( context, intent) ;
}
} catch ( SecurityException e) {
Intent intent = new Intent ( ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
ComponentName comp = new ComponentName ( "com.huawei.systemmanager" ,
"com.huawei.permissionmanager.ui.MainActivity" ) ;
intent. setComponent ( comp) ;
startActivity ( context, intent) ;
Log . e ( TAG, Log . getStackTraceString ( e) ) ;
} catch ( ActivityNotFoundException e) {
Intent intent = new Intent ( ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
ComponentName comp = new ComponentName ( "com.Android.settings" , "com.android.settings.permission.TabItem" ) ;
intent. setComponent ( comp) ;
startActivity ( context, intent) ;
Log . e ( TAG, Log . getStackTraceString ( e) ) ;
} catch ( Exception e) {
Log . e ( TAG, Log . getStackTraceString ( e) ) ;
}
return false ;
}
private void startActivity ( Context context, Intent intent) {
context. startActivity ( intent) ;
}
}
12.3 魅族悬浮窗权限兼容实现
import android. content. Context ;
import android. content. Intent ;
import android. util. Log ;
public class MeizuCompatImpl extends BelowApi23CompatImpl {
@Override
public boolean isSupported ( ) {
return true ;
}
@Override
public boolean apply ( Context context) {
try {
Intent intent = new Intent ( "com.meizu.safe.security.SHOW_APPSEC" ) ;
intent. setClassName ( "com.meizu.safe" , "com.meizu.safe.security.AppSecActivity" ) ;
intent. putExtra ( "packageName" , context. getPackageName ( ) ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
startActivity ( context, intent) ;
} catch ( Exception e) {
try {
Log . d ( "dq" , "flyme 6.2.5+,apply permission failed" ) ;
Api23CompatImpl . commonROMPermissionApplyInternal ( context) ;
} catch ( Exception eFinal) {
eFinal. printStackTrace ( ) ;
}
}
return true ;
}
private void startActivity ( Context context, Intent intent) {
context. startActivity ( intent) ;
}
}
12.4 MIUI 悬浮窗权限兼容实现
import android. content. Context ;
import android. content. Intent ;
import android. net. Uri ;
import android. provider. Settings ;
import android. util. Log ;
public class MiuiCompatImpl extends BelowApi23CompatImpl {
private static final String TAG = "MiuiCompatImpl" ;
private int miuiVersion = - 1 ;
public MiuiCompatImpl ( ) {
miuiVersion = RomUtils . getMiuiVersion ( ) ;
}
@Override
public boolean isSupported ( ) {
return miuiVersion >= 5 && miuiVersion <= 8 ;
}
@Override
public boolean apply ( Context context) {
if ( miuiVersion == 5 ) {
return applyV5 ( context) ;
} else if ( miuiVersion == 6 ) {
return applyV6 ( context) ;
} else if ( miuiVersion == 7 ) {
return applyV7 ( context) ;
} else if ( miuiVersion == 8 ) {
return applyV8 ( context) ;
} else {
Log . e ( TAG, "this is a special MIUI rom version, its version code " + miuiVersion) ;
}
return false ;
}
private boolean applyV8 ( Context context) {
Intent intent = new Intent ( "miui.intent.action.APP_PERM_EDITOR" ) ;
intent. setClassName ( "com.miui.securitycenter" , "com.miui.permcenter.permissions.PermissionsEditorActivity" ) ;
intent. putExtra ( "extra_pkgname" , context. getPackageName ( ) ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
if ( RomUtils . isIntentAvailable ( context, intent) ) {
startActivity ( context, intent) ;
return true ;
}
intent = new Intent ( "miui.intent.action.APP_PERM_EDITOR" ) ;
intent. setPackage ( "com.miui.securitycenter" ) ;
intent. putExtra ( "extra_pkgname" , context. getPackageName ( ) ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
if ( RomUtils . isIntentAvailable ( context, intent) ) {
startActivity ( context, intent) ;
return true ;
} else {
return applyV5 ( context) ;
}
}
private boolean applyV7 ( Context context) {
Intent intent = new Intent ( "miui.intent.action.APP_PERM_EDITOR" ) ;
intent. setClassName ( "com.miui.securitycenter" , "com.miui.permcenter.permissions.AppPermissionsEditorActivity" ) ;
intent. putExtra ( "extra_pkgname" , context. getPackageName ( ) ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
if ( RomUtils . isIntentAvailable ( context, intent) ) {
startActivity ( context, intent) ;
return true ;
} else {
Log . e ( TAG, "applyV7 Intent is not available!" ) ;
}
return false ;
}
private boolean applyV6 ( Context context) {
Intent intent = new Intent ( "miui.intent.action.APP_PERM_EDITOR" ) ;
intent. setClassName ( "com.miui.securitycenter" , "com.miui.permcenter.permissions.AppPermissionsEditorActivity" ) ;
intent. putExtra ( "extra_pkgname" , context. getPackageName ( ) ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
if ( RomUtils . isIntentAvailable ( context, intent) ) {
startActivity ( context, intent) ;
return true ;
} else {
Log . e ( TAG, "applyV6 Intent is not available!" ) ;
}
return false ;
}
private boolean applyV5 ( Context context) {
Intent intent;
String packageName = context. getPackageName ( ) ;
intent = new Intent ( Settings . ACTION_APPLICATION_DETAILS_SETTINGS) ;
Uri uri = Uri . fromParts ( "package" , packageName, null ) ;
intent. setData ( uri) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
if ( RomUtils . isIntentAvailable ( context, intent) ) {
startActivity ( context, intent) ;
return true ;
} else {
Log . e ( TAG, "applyV5 intent is not available!" ) ;
}
return false ;
}
private void startActivity ( Context context, Intent intent) {
context. startActivity ( intent) ;
}
}
12.5 360 悬浮窗权限兼容实现
import android. content. Context ;
import android. content. Intent ;
import android. util. Log ;
public class QihooCompatImpl extends BelowApi23CompatImpl {
private static final String TAG = "QihooCompatImpl" ;
@Override
public boolean isSupported ( ) {
return true ;
}
@Override
public boolean apply ( Context context) {
Intent intent = new Intent ( ) ;
intent. setClassName ( "com.android.settings" , "com.android.settings.Settings$OverlaySettingsActivity" ) ;
intent. setFlags ( Intent . FLAG_ACTIVITY_NEW_TASK) ;
if ( RomUtils . isIntentAvailable ( context, intent) ) {
startActivity ( context, intent) ;
return true ;
} else {
intent. setClassName ( "com.qihoo360.mobilesafe" , "com.qihoo360.mobilesafe.ui.index.appEnterActivity" ) ;
if ( RomUtils . isIntentAvailable ( context, intent) ) {
startActivity ( context, intent) ;
return true ;
} else {
Log . e ( TAG, "can't open permission page with particular name, please use " +
"\"adb shell dumpsys activity\" command and tell me the name of the float window permission page" ) ;
}
}
return false ;
}
private void startActivity ( Context context, Intent intent) {
context. startActivity ( intent) ;
}
}
12.6 Rom 工具类
import android. content. Context ;
import android. content. Intent ;
import android. content. pm. PackageManager ;
import android. os. Build ;
import android. text. TextUtils ;
import android. util. Log ;
import java. io. BufferedReader ;
import java. io. IOException ;
import java. io. InputStreamReader ;
public class RomUtils {
private static final String TAG = RomUtils . class . getSimpleName ( ) ;
public static double getEmuiVersion ( ) {
try {
String emuiVersion = getSystemProperty ( "ro.build.version.emui" ) ;
String version = emuiVersion. substring ( emuiVersion. indexOf ( "_" ) + 1 ) ;
return Double . parseDouble ( version) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
return 4.0 ;
}
public static int getMiuiVersion ( ) {
String version = getSystemProperty ( "ro.miui.ui.version.name" ) ;
if ( version != null ) {
try {
return Integer . parseInt ( version. substring ( 1 ) ) ;
} catch ( Exception e) {
Log . e ( TAG, "getInstance miui version code error, version : " + version) ;
}
}
return - 1 ;
}
public static String getSystemProperty ( String propName) {
String line;
BufferedReader input = null ;
try {
Process p = Runtime . getRuntime ( ) . exec ( "getprop " + propName) ;
input = new BufferedReader ( new InputStreamReader ( p. getInputStream ( ) ) , 1024 ) ;
line = input. readLine ( ) ;
input. close ( ) ;
} catch ( IOException ex) {
Log . e ( TAG, "Unable to read sysprop " + propName, ex) ;
return null ;
} finally {
if ( input != null ) {
try {
input. close ( ) ;
} catch ( IOException e) {
Log . e ( TAG, "Exception while closing InputStream" , e) ;
}
}
}
return line;
}
public static boolean isOppo ( ) {
return ! TextUtils . isEmpty ( getSystemProperty ( "ro.build.version.opporom" ) ) ;
}
public static boolean isVivo ( ) {
return ! TextUtils . isEmpty ( getSystemProperty ( "ro.vivo.os.version" ) ) ;
}
public static boolean isHuawei ( ) {
return Build . MANUFACTURER. contains ( "HUAWEI" ) ;
}
public static boolean isMiui ( ) {
return ! TextUtils . isEmpty ( getSystemProperty ( "ro.miui.ui.version.name" ) ) ;
}
public static boolean isMeizu ( ) {
String meizuFlymeOSFlag = getSystemProperty ( "ro.build.display.id" ) ;
return ! TextUtils . isEmpty ( meizuFlymeOSFlag) && meizuFlymeOSFlag. toLowerCase ( ) . contains ( "flyme" ) ;
}
public static boolean isQihoo ( ) {
return Build . MANUFACTURER. contains ( "QiKU" ) ;
}
public static boolean isIntentAvailable ( Context context, Intent intent) {
if ( intent == null ) {
return false ;
}
return context. getPackageManager ( ) . queryIntentActivities ( intent, PackageManager . MATCH_DEFAULT_ONLY) . size ( ) > 0 ;
}
}
13.击碎爆破粒子
public class ExplosionAnimator extends ValueAnimator {
public static final int DEFAULT_DURATION = 1500 ;
private Particle [ ] [ ] mParticles;
private Paint mPaint;
private View mContainer;
public ExplosionAnimator ( View view, Bitmap bitmap, Rect bound) {
mPaint = new Paint ( ) ;
mContainer = view;
setFloatValues ( 0.0f , 1.0f ) ;
setDuration ( DEFAULT_DURATION) ;
mParticles = generateParticles ( bitmap, bound) ;
}
private Particle [ ] [ ] generateParticles ( Bitmap bitmap, Rect bound) {
int w = bound. width ( ) ;
int h = bound. height ( ) ;
int partW_Count = w / Particle . PART_WH;
int partH_Count = h / Particle . PART_WH;
int bitmap_part_w = bitmap. getWidth ( ) / partW_Count;
int bitmap_part_h = bitmap. getHeight ( ) / partH_Count;
Particle [ ] [ ] particles = new Particle [ partH_Count] [ partW_Count] ;
Point point = null ;
for ( int row = 0 ; row < partH_Count; row ++ ) {
for ( int column = 0 ; column < partW_Count; column ++ ) {
int color = bitmap. getPixel ( column * bitmap_part_w, row * bitmap_part_h) ;
point = new Point ( column, row) ;
particles[ row] [ column] = Particle . generateParticle ( color, bound, point) ;
}
}
return particles;
}
public void draw ( Canvas canvas) {
if ( ! isStarted ( ) ) {
return ;
}
for ( Particle [ ] particle : mParticles) {
for ( Particle p : particle) {
p. advance ( ( Float ) getAnimatedValue ( ) ) ;
mPaint. setColor ( p. color) ;
mPaint. setAlpha ( ( int ) ( Color . alpha ( p. color) * p. alpha) ) ;
canvas. drawCircle ( p. cx, p. cy, p. radius, mPaint) ;
}
}
mContainer. invalidate ( ) ;
}
@Override
public void start ( ) {
super . start ( ) ;
mContainer. invalidate ( ) ;
}
}
import android. animation. Animator ;
import android. animation. AnimatorListenerAdapter ;
import android. app. Activity ;
import android. content. Context ;
import android. graphics. Bitmap ;
import android. graphics. Canvas ;
import android. graphics. Rect ;
import android. util. AttributeSet ;
import android. view. View ;
import android. view. ViewGroup ;
import android. view. Window ;
import com. example. amusic. utils. posui. utils. Utils ;
import java. util. ArrayList ;
public class ExplosionField extends View {
private static final String TAG = "ExplosionField" ;
private static final Canvas mCanvas = new Canvas ( ) ;
private ArrayList < ExplosionAnimator > explosionAnimators;
private OnClickListener onClickListener;
public ExplosionField ( Context context) {
super ( context) ;
init ( ) ;
}
public ExplosionField ( Context context, AttributeSet attrs) {
super ( context, attrs) ;
init ( ) ;
}
private void init ( ) {
explosionAnimators = new ArrayList < ExplosionAnimator > ( ) ;
attach2Activity ( ( Activity ) getContext ( ) ) ;
}
@Override
protected void onDraw ( Canvas canvas) {
super . onDraw ( canvas) ;
for ( ExplosionAnimator animator : explosionAnimators) {
animator. draw ( canvas) ;
}
}
public void explode ( final View view) {
Rect rect = new Rect ( ) ;
view. getGlobalVisibleRect ( rect) ;
rect. offset ( 0 , - Utils . dp2px ( 25 ) ) ;
final ExplosionAnimator animator = new ExplosionAnimator ( this , createBitmapFromView ( view) , rect) ;
explosionAnimators. add ( animator) ;
animator. addListener ( new AnimatorListenerAdapter ( ) {
@Override
public void onAnimationStart ( Animator animation) {
view. animate ( ) . alpha ( 0f ) . setDuration ( 150 ) . start ( ) ;
}
@Override
public void onAnimationEnd ( Animator animation) {
view. animate ( ) . alpha ( 1f ) . setDuration ( 150 ) . start ( ) ;
explosionAnimators. remove ( animation) ;
animation = null ;
}
} ) ;
animator. start ( ) ;
}
private Bitmap createBitmapFromView ( View view) {
Bitmap bitmap = Bitmap . createBitmap ( view. getWidth ( ) , view. getHeight ( ) , Bitmap. Config . ARGB_8888) ;
if ( bitmap != null ) {
synchronized ( mCanvas) {
mCanvas. setBitmap ( bitmap) ;
view. draw ( mCanvas) ;
mCanvas. setBitmap ( null ) ;
}
}
return bitmap;
}
private void attach2Activity ( Activity activity) {
ViewGroup rootView = ( ViewGroup ) activity. findViewById ( Window . ID_ANDROID_CONTENT) ;
ViewGroup. LayoutParams lp = new ViewGroup. LayoutParams (
ViewGroup. LayoutParams . MATCH_PARENT, ViewGroup. LayoutParams . MATCH_PARENT) ;
rootView. addView ( this , lp) ;
}
public void addListener ( View view) {
if ( view instanceof ViewGroup ) {
ViewGroup viewGroup = ( ViewGroup ) view;
int count = viewGroup. getChildCount ( ) ;
for ( int i = 0 ; i < count; i++ ) {
addListener ( viewGroup. getChildAt ( i) ) ;
}
} else {
view. setClickable ( true ) ;
view. setOnClickListener ( getOnClickListener ( ) ) ;
}
}
private OnClickListener getOnClickListener ( ) {
if ( null == onClickListener) {
onClickListener = new OnClickListener ( ) {
@Override
public void onClick ( View v) {
ExplosionField . this . explode ( v) ;
}
} ;
}
return onClickListener;
}
}
import android. graphics. Point ;
import android. graphics. Rect ;
import java. util. Random ;
public class Particle {
public static final int PART_WH = 8 ;
float cx;
float cy;
float radius;
int color;
float alpha;
static Random random = new Random ( ) ;
Rect mBound;
public static Particle generateParticle ( int color, Rect bound, Point point) {
int row = point. y;
int column = point. x;
Particle particle = new Particle ( ) ;
particle. mBound = bound;
particle. color = color;
particle. alpha = 1f ;
particle. radius = PART_WH;
particle. cx = bound. left + PART_WH * column;
particle. cy = bound. top + PART_WH * row;
return particle;
}
public void advance ( float factor) {
cx = cx + factor * random. nextInt ( mBound. width ( ) ) * ( random. nextFloat ( ) - 0.5f ) ;
cy = cy + factor * random. nextInt ( mBound. height ( ) / 2 ) ;
radius = radius - factor * random. nextInt ( 2 ) ;
alpha = ( 1f - factor) * ( 1 + random. nextFloat ( ) ) ;
}
}
import android. content. res. Resources ;
public class Utils {
public static final float DENSITY = Resources . getSystem ( ) . getDisplayMetrics ( ) . density;
public static int dp2px ( int dp) {
return Math . round ( dp * DENSITY) ;
}
}
13.1 击碎爆破粒子使用
ExplosionField explosionField = new ExplosionField ( mContext) ;
explosionField. addListener ( view. findViewById ( R . id. rootd1) ) ;
14.WindowUtils工具类
import android. annotation. TargetApi ;
import android. app. Activity ;
import android. content. Context ;
import android. content. ContextWrapper ;
import android. content. pm. ActivityInfo ;
import android. content. res. Resources ;
import android. content. res. TypedArray ;
import android. graphics. PixelFormat ;
import android. os. Build ;
import android. util. Log ;
import android. view. View ;
import android. view. ViewConfiguration ;
import android. view. ViewGroup ;
import android. view. Window ;
import android. view. WindowManager ;
import java. lang. reflect. Field ;
import java. lang. reflect. Method ;
import static android. content. Context . WINDOW_SERVICE;
import static android. view. ViewGroup. LayoutParams . MATCH_PARENT;
public class WindowUtils {
public static boolean isTranslucentOrFloating ( Context context) {
boolean isTranslucentOrFloating = false ;
try {
int [ ] styleableRes = ( int [ ] ) Class . forName ( "com.android.internal.R$styleable" ) . getField ( "Window" ) . get ( null ) ;
final TypedArray ta = context. obtainStyledAttributes ( styleableRes) ;
Method m = ActivityInfo . class . getMethod ( "isTranslucentOrFloating" , TypedArray . class ) ;
m. setAccessible ( true ) ;
isTranslucentOrFloating = ( boolean ) m. invoke ( null , ta) ;
m. setAccessible ( false ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
return isTranslucentOrFloating;
}
public static void fixOrientation ( Activity activity) {
try {
Field field = Activity . class . getDeclaredField ( "mActivityInfo" ) ;
field. setAccessible ( true ) ;
ActivityInfo o = ( ActivityInfo ) field. get ( activity) ;
o. screenOrientation = - 1 ;
field. setAccessible ( false ) ;
} catch ( Exception e) {
e. printStackTrace ( ) ;
}
}
public static int getScreenWidth ( Context context) {
return context. getResources ( ) . getDisplayMetrics ( ) . widthPixels;
}
public static int getScreenHeight ( Context context) {
return context. getResources ( ) . getDisplayMetrics ( ) . heightPixels;
}
public static int getNavigationBarHeight ( Context context) {
int result = 0 ;
if ( hasNavBar ( context) ) {
Resources res = context. getResources ( ) ;
int resourceId = res. getIdentifier ( "navigation_bar_height" , "dimen" , "android" ) ;
if ( resourceId > 0 ) {
result = res. getDimensionPixelSize ( resourceId) ;
}
}
return result;
}
@TargetApi ( Build . VERSION_CODES. ICE_CREAM_SANDWICH)
public static boolean hasNavBar ( Context context) {
Resources res = context. getResources ( ) ;
int resourceId = res. getIdentifier ( "config_showNavigationBar" , "bool" , "android" ) ;
if ( resourceId != 0 ) {
boolean hasNav = res. getBoolean ( resourceId) ;
String sNavBarOverride = getNavBarOverride ( ) ;
if ( "1" . equals ( sNavBarOverride) ) {
hasNav = false ;
} else if ( "0" . equals ( sNavBarOverride) ) {
hasNav = true ;
}
return hasNav;
} else {
return ! ViewConfiguration . get ( context) . hasPermanentMenuKey ( ) ;
}
}
private static String getNavBarOverride ( ) {
String sNavBarOverride = null ;
if ( Build . VERSION. SDK_INT >= Build . VERSION_CODES. KITKAT) {
try {
Class c = Class . forName ( "android.os.SystemProperties" ) ;
Method m = c. getDeclaredMethod ( "get" , String . class ) ;
m. setAccessible ( true ) ;
sNavBarOverride = ( String ) m. invoke ( null , "qemu.hw.mainkeys" ) ;
} catch ( Throwable e) {
}
}
return sNavBarOverride;
}
public static void setHideVirtualKey ( Window window) {
int uiOptions = View . SYSTEM_UI_FLAG_LAYOUT_STABLE|
View . SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION|
View . SYSTEM_UI_FLAG_FULLSCREEN|
View . SYSTEM_UI_FLAG_HIDE_NAVIGATION|
View . SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
if ( Build . VERSION. SDK_INT >= 19 ) {
uiOptions |= 0x00001000 ;
} else {
uiOptions |= View . SYSTEM_UI_FLAG_LOW_PROFILE;
}
window. getDecorView ( ) . setSystemUiVisibility ( uiOptions) ;
}
public static void setBackgroundAlpha ( Activity activity, float bgAlpha) {
WindowManager. LayoutParams lp = activity. getWindow ( ) . getAttributes ( ) ;
lp. alpha = bgAlpha;
if ( bgAlpha == 1 ) {
activity. getWindow ( ) . clearFlags ( WindowManager. LayoutParams . FLAG_DIM_BEHIND) ;
} else {
activity. getWindow ( ) . addFlags ( WindowManager. LayoutParams . FLAG_DIM_BEHIND) ;
}
activity. getWindow ( ) . setAttributes ( lp) ;
}
public static int getStatusBarHeight ( Context context) {
int statusBarHeight = 0 ;
Resources res = context. getResources ( ) ;
int resourceId = res. getIdentifier ( "status_bar_height" , "dimen" , "android" ) ;
if ( resourceId > 0 ) {
statusBarHeight = res. getDimensionPixelSize ( resourceId) ;
}
return statusBarHeight;
}
public static void enterFullScreen ( Context context, View view) {
ViewGroup parent = ( ViewGroup ) view. getParent ( ) ;
if ( parent != null ) {
parent. removeView ( view) ;
}
ViewGroup contentView = scanForActivity ( context)
. findViewById ( android. R. id. content) ;
ViewGroup. LayoutParams params = new ViewGroup. LayoutParams (
MATCH_PARENT, MATCH_PARENT) ;
contentView. addView ( view, params) ;
}
public static void exitFullScreen ( Context context, View view) {
ViewGroup contentView = scanForActivity ( context)
. findViewById ( android. R. id. content) ;
contentView. removeView ( view) ;
}
private static Activity scanForActivity ( Context cont) {
if ( cont == null ) {
Log . d ( "scanForActivity" , "cont == null" ) ;
return null ;
} else if ( cont instanceof Activity ) {
Log . d ( "scanForActivity" , "Activity" ) ;
return ( Activity ) cont;
} else if ( cont instanceof ContextWrapper ) {
Log . d ( "scanForActivity" , "ContextWrapper" ) ;
return scanForActivity ( ( ( ContextWrapper ) cont) . getBaseContext ( ) ) ;
}
Log . d ( "scanForActivity" , "not result" ) ;
return null ;
}
public static WindowManager. LayoutParams newWmParams ( int width, int height) {
WindowManager. LayoutParams params = new WindowManager. LayoutParams ( ) ;
params. flags = WindowManager. LayoutParams . FLAG_NOT_TOUCH_MODAL
| WindowManager. LayoutParams . FLAG_KEEP_SCREEN_ON
| WindowManager. LayoutParams . FLAG_NOT_FOCUSABLE
| WindowManager. LayoutParams . FLAG_SCALED
| WindowManager. LayoutParams . FLAG_LAYOUT_INSET_DECOR
| WindowManager. LayoutParams . FLAG_LAYOUT_IN_SCREEN;
if ( Build . VERSION. SDK_INT >= 26 ) {
params. type = WindowManager. LayoutParams . TYPE_APPLICATION_OVERLAY;
} else {
params. type = WindowManager. LayoutParams . TYPE_SYSTEM_ALERT;
}
params. width = width;
params. height = height;
params. format = PixelFormat . TRANSLUCENT;
return params;
}
public static WindowManager getWindowManager ( Context context) {
return ( WindowManager ) context. getSystemService ( WINDOW_SERVICE) ;
}
}
15.常用框架-PhotoView缩放图片
implementation 'com.github.chrisbanes:PhotoView:1.2.6'
private void ImaEvent ( ) {
PhotoView photoView= findViewById ( R . id. id_myimg) ;
Button xiazai= findViewById ( R . id. button13) ;
PicassoUtils . loadImageViewCrop2 ( paihang. this , localhost_path+ "/image/lagelangri/paihangpic.png" , photoView) ;
xiazai. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
Uri uri = Uri . parse ( localhost_path+ "/image/lagelangri/paihangpic.png" ) ;
Intent intent = new Intent ( Intent . ACTION_VIEW, uri) ;
startActivity ( intent) ;
}
} ) ;
}
< uk. co. senab. photoview. PhotoView
android: id= "@+id/id_myimg"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
android: layout_gravity= "center" / >
16.常用框架-加载gif
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.7'
< pl. droidsonroids. gif. GifImageView
android: id= "@+id/loading_img"
android: layout_width= "match_parent"
android: layout_height= "450dp"
android: layout_gravity= "center_vertical"
android: layout_marginTop= "80dp"
android: src= "@drawable/wel" / >
17.判断App是第一次启动
SharedPreferences setting;
setting = getSharedPreferences ( SHARE_APP_TAG, 0 ) ;
private void first ( ) {
Boolean user_first = setting. getBoolean ( "FIRST21" , true ) ;
if ( user_first) {
setting. edit ( ) . putBoolean ( "FIRST21" , false ) . commit ( ) ;
setting. edit ( ) . putString ( "userName" , "null" ) . commit ( ) ;
setting. edit ( ) . putString ( "passWord" , "null" ) . commit ( ) ;
setting. edit ( ) . putString ( "devicecode" , getPassword ( ) ) . commit ( ) ;
DEVICECODE= setting. getString ( "devicecode" , "" ) ;
Intent intent= new Intent ( welcome. this , Login . class ) ;
startActivity ( intent) ;
} else {
uphone= setting. getString ( "userName" , "" ) ;
upassword= setting. getString ( "passWord" , "" ) ;
DEVICECODE= setting. getString ( "devicecode" , "" ) ;
newRunner ( ) ;
}
}
18.sqlite数据库
import android. content. Context ;
import android. database. sqlite. SQLiteDatabase ;
import android. database. sqlite. SQLiteDatabase. CursorFactory ;
import android. database. sqlite. SQLiteOpenHelper ;
public class MyDatabase extends SQLiteOpenHelper {
public static final String LEARN_HISTORY = "create table Learn(id integer primary key autoincrement,kcid char(20),listid char(20))" ;
public static final String LOGINUSER = "create table LoginUser(u_id integer,userName char(100),passWord char(20),name char(20),headImage char(100),sex char(20),major char(20),grade char(20),identity char(20),phone char(20),mibao char(20))" ;
private Context mContext;
public MyDatabase ( Context context, String name, CursorFactory factory,
int version) {
super ( context, name, factory, version) ;
mContext = context ;
}
@Override
public void onCreate ( SQLiteDatabase db) {
db. execSQL ( LEARN_HISTORY) ;
db. execSQL ( LOGINUSER) ;
}
@Override
public void onUpgrade ( SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
18.1 简单增删改查
private MyDatabase myDatebaseHelper;
myDatebaseHelper = new MyDatabase ( this , SQLITE_NAME, null , 1 ) ;
public boolean IScheck ( String phone) {
SQLiteDatabase db = myDatebaseHelper. getWritableDatabase ( ) ;
String Query = "Select * from LoginUser where phone =?" ;
Cursor cursor = db. rawQuery ( Query , new String [ ] {
phone} ) ;
if ( cursor. getCount ( ) > 0 ) {
cursor. close ( ) ;
return true ;
}
cursor. close ( ) ;
return false ;
}
public void del ( ) {
SQLiteDatabase db = myDatebaseHelper. getWritableDatabase ( ) ;
db. delete ( "LoginUser" , "phone=?" , new String [ ] {
USERS. getPhone ( ) } ) ;
db. close ( ) ;
addUser ( ) ;
}
private void less ( ) {
SQLiteDatabase db = myDatebaseHelper. getWritableDatabase ( ) ;
ContentValues values = new ContentValues ( ) ;
values. put ( "u_id" , USERS. getU_id ( ) ) ;
values. put ( "userName" , USERS. getUserName ( ) ) ;
values. put ( "passWord" , USERS. getPassWord ( ) ) ;
values. put ( "name" , USERS. getName ( ) ) ;
values. put ( "headImage" , USERS. getHeadImage ( ) ) ;
values. put ( "sex" , USERS. getSex ( ) ) ;
values. put ( "major" , USERS. getMajor ( ) ) ;
values. put ( "grade" , USERS. getGrade ( ) ) ;
values. put ( "identity" , USERS. getIdentity ( ) ) ;
values. put ( "phone" , USERS. getPhone ( ) ) ;
values. put ( "mibao" , String . valueOf ( USERS. getMibao ( ) ) ) ;
db. update ( "LoginUser" , values, "phone=?" , new String [ ] {
USERS. getPhone ( ) } ) ;
db. close ( ) ;
}
private void addUser ( ) {
SQLiteDatabase db = myDatebaseHelper. getWritableDatabase ( ) ;
ContentValues values = new ContentValues ( ) ;
values. put ( "u_id" , USERS. getU_id ( ) ) ;
values. put ( "userName" , USERS. getUserName ( ) ) ;
values. put ( "passWord" , USERS. getPassWord ( ) ) ;
values. put ( "name" , USERS. getName ( ) ) ;
values. put ( "headImage" , USERS. getHeadImage ( ) ) ;
values. put ( "sex" , USERS. getSex ( ) ) ;
values. put ( "major" , USERS. getMajor ( ) ) ;
values. put ( "grade" , USERS. getGrade ( ) ) ;
values. put ( "identity" , USERS. getIdentity ( ) ) ;
values. put ( "phone" , USERS. getPhone ( ) ) ;
values. put ( "mibao" , String . valueOf ( USERS. getMibao ( ) ) ) ;
db. insert ( "LoginUser" , null , values) ;
db. close ( ) ;
}
19.流式布局原理
private void addInit ( ) {
int w, h;
WindowManager wm = ( WindowManager ) getSystemService ( Context . WINDOW_SERVICE) ;
w = getAndroiodScreenWidth ( wm) ;
for ( int i= 0 ; i< ls. size ( ) ; i++ ) {
final ImageView imageView = new ImageView ( this ) ;
final FrameLayout f= new FrameLayout ( this ) ;
final TextView t= new TextView ( this ) ;
FrameLayout. LayoutParams params = new FrameLayout. LayoutParams ( w/ 5 - 20 , w/ 5 - 20 ) ;
t. setGravity ( Gravity . CENTER) ;
imageView. setAlpha ( 0.8f ) ;
t. setTextColor ( this . getColor ( R . color. ffffff) ) ;
t. setText ( "未获得" ) ;
for ( int j= 0 ; j< ls2. size ( ) ; j++ ) {
if ( ls. get ( i) . getHeadImage ( ) . equals ( ls2. get ( j) . getHeadImage ( ) ) ) {
imageView. setAlpha ( 1f ) ;
t. setVisibility ( View . GONE) ;
}
}
PicassoUtils . loadImageViewCrop2 ( ChangeHeadImage . this , localhost_path+ ls. get ( i) . getHeadImage ( ) , imageView) ;
f. addView ( imageView) ;
f. addView ( t) ;
gridLayout. addView ( f, params) ;
final int finalI = i;
imageView. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
if ( imageView. getAlpha ( ) == 1f ) {
headImages= ls. get ( finalI) . getHeadImage ( ) ;
HEADIMA= 1 ;
updateUserIma ( ) ;
} else {
Toast . makeText ( ChangeHeadImage . this , "未获得" , Toast . LENGTH_SHORT) . show ( ) ;
}
}
} ) ;
}
}
20.监听屏幕滑动简单原理
@Override
public boolean onTouchEvent ( MotionEvent event) {
if ( event. getAction ( ) == MotionEvent . ACTION_DOWN) {
x1 = event. getX ( ) ;
y1 = event. getY ( ) ;
}
if ( event. getAction ( ) == MotionEvent . ACTION_UP) {
x2 = event. getX ( ) ;
y2 = event. getY ( ) ;
if ( y1 - y2 > 50 ) {
Toast . makeText ( MainActivity . this , "向上滑" , Toast . LENGTH_SHORT) . show ( ) ;
} else if ( y2 - y1 > 50 ) {
Toast . makeText ( MainActivity . this , "向下滑" , Toast . LENGTH_SHORT) . show ( ) ;
} else if ( x1 - x2 > 50 ) {
Toast . makeText ( MainActivity . this , "向左滑" , Toast . LENGTH_SHORT) . show ( ) ;
} else if ( x2 - x1 > 50 ) {
Toast . makeText ( MainActivity . this , "向右滑" , Toast . LENGTH_SHORT) . show ( ) ;
}
}
return super . onTouchEvent ( event) ;
}
21.Listview任意行生成截图分享
public static Bitmap createBitmap ( ListView listView, Context context) {
int titleHeight, width, height, rootHeight= 0 ;
Bitmap bitmap;
Canvas canvas;
int yPos = 0 ;
int listItemNum;
List < View > childViews = null ;
width = 1200 ;
ListAdapter listAdapter = listView. getAdapter ( ) ;
listItemNum = listAdapter. getCount ( ) ;
childViews = new ArrayList < View > ( listItemNum) ;
View itemView;
if ( JTALL== 0 ) {
for ( int pos= 0 ; pos < jt. size ( ) ; ++ pos) {
itemView = listAdapter. getView ( jt. get ( pos) , null , listView) ;
itemView. measure ( View. MeasureSpec . makeMeasureSpec ( width, View. MeasureSpec . EXACTLY) , View. MeasureSpec . makeMeasureSpec ( 0 , View. MeasureSpec . UNSPECIFIED) ) ;
childViews. add ( itemView) ;
rootHeight += itemView. getMeasuredHeight ( ) ;
}
} else {
for ( int pos= 0 ; pos < listItemNum; ++ pos) {
itemView = listAdapter. getView ( pos, null , listView) ;
itemView. measure ( View. MeasureSpec . makeMeasureSpec ( width, View. MeasureSpec . EXACTLY) , View. MeasureSpec . makeMeasureSpec ( 0 , View. MeasureSpec . UNSPECIFIED) ) ;
childViews. add ( itemView) ;
rootHeight += itemView. getMeasuredHeight ( ) ;
}
JTALL= 0 ;
}
bitmap = Bitmap . createBitmap ( width, rootHeight,
Bitmap. Config . ARGB_8888) ;
canvas = new Canvas ( bitmap) ;
Bitmap itemBitmap;
int childHeight;
for ( int pos= 0 ; pos < childViews. size ( ) ; ++ pos) {
itemView = childViews. get ( pos) ;
childHeight = itemView. getMeasuredHeight ( ) ;
itemBitmap = viewToBitmap ( itemView, width, childHeight) ;
if ( itemBitmap!= null ) {
canvas. drawBitmap ( itemBitmap, 0 , yPos, null ) ;
}
yPos = childHeight + yPos;
}
canvas. save ( ) ;
canvas. restore ( ) ;
return bitmap;
}
private static Bitmap viewToBitmap ( View view, int viewWidth, int viewHeight) {
view. layout ( 0 , 0 , viewWidth, viewHeight) ;
view. buildDrawingCache ( ) ;
Bitmap bitmap = view. getDrawingCache ( ) ;
return bitmap;
}
21.1 分享
Uri uri = Uri . parse ( MediaStore. Images. Media . insertImage ( getContentResolver ( ) , ima, null , null ) ) ;
private void share ( Uri uri) {
Intent shareIntent = new Intent ( Intent . ACTION_SEND) ;
shareIntent. putExtra ( Intent . EXTRA_TEXT, "hello world" ) ;
shareIntent. setType ( "text/plain" ) ;
shareIntent. putExtra ( Intent . EXTRA_STREAM, uri) ;
shareIntent. setType ( "image/*" ) ;
startActivity ( Intent . createChooser ( shareIntent, "activityTitle" ) ) ;
}
22.获取当前本地apk的版本/版本号名称
import android. content. Context ;
import android. content. pm. PackageManager ;
public class APKVersionCodeUtils {
public static int getVersionCode ( Context mContext) {
int versionCode = 0 ;
try {
versionCode = mContext. getPackageManager ( ) .
getPackageInfo ( mContext. getPackageName ( ) , 0 ) . versionCode;
} catch ( PackageManager. NameNotFoundException e) {
e. printStackTrace ( ) ;
}
return versionCode;
}
public static String getVerName ( Context context) {
String verName = "" ;
try {
verName = context. getPackageManager ( ) .
getPackageInfo ( context. getPackageName ( ) , 0 ) . versionName;
} catch ( PackageManager. NameNotFoundException e) {
e. printStackTrace ( ) ;
}
return verName;
}
}
23.ImageView高度根据图片比例自适应
< ImageView
android: id= "@+id/iv_test"
android: layout_width= "fill_parent"
android: layout_height= "wrap_content"
android: adjustViewBounds= "true"
android: scaleType= "fitXY"
/ >
24.ToastUtil封装类
import android. annotation. SuppressLint ;
import android. content. Context ;
import android. widget. Toast ;
public class ToastUtil {
@SuppressLint ( "StaticFieldLeak" )
private static Context mContext;
public static void init ( Context context) {
mContext = context. getApplicationContext ( ) ;
}
public static void show ( String content) {
Toast . makeText ( mContext, content, Toast . LENGTH_SHORT) . show ( ) ;
}
}
24.1 ToastUtil封装类使用
ToastUtil . show ( "23232---3232" ) ;
25.Android四大组件-广播入门
25.1 BroadcastReceiver-广播判断网络状态
public class MyNetworkReceiver extends BroadcastReceiver {
@Override
public void onReceive ( Context context, Intent intent) {
boolean isconnect = false ;
ConnectivityManager connectivityManager = ( ConnectivityManager ) context. getSystemService ( context. CONNECTIVITY_SERVICE) ;
NetworkInfo. State state = connectivityManager. getNetworkInfo ( ConnectivityManager . TYPE_WIFI) . getState ( ) ;
if ( state == NetworkInfo. State . CONNECTED) {
isconnect = true ;
Toast . makeText ( context , "wifi连接" , Toast . LENGTH_LONG) . show ( ) ;
}
NetworkInfo. State gstate = connectivityManager. getNetworkInfo ( ConnectivityManager . TYPE_MOBILE) . getState ( ) ;
if ( gstate == NetworkInfo. State . CONNECTED) {
isconnect = true ;
Toast . makeText ( context , "当前正在使用数据流量" , Toast . LENGTH_LONG) . show ( ) ;
}
if ( ! isconnect) {
Toast . makeText ( context , "无网络连接" , Toast . LENGTH_LONG) . show ( ) ;
}
}
}
```java
26.EditText的监听-TextWatcher三个回调
EditText et = ( EditText ) findViewById ( R . id. et) ;
TextWatcher watcher = new TextWatcher ( ) {
@Override
public void beforeTextChanged ( CharSequence s, int start, int count, int after) {
Log . d ( TAG, "beforeTextChanged: s = " + s + ", start = " + start +
", count = " + count + ", after = " + after) ;
}
@Override
public void onTextChanged ( CharSequence s, int start, int before, int count) {
Log . d ( TAG, "onTextChanged: s = " + s + ", start = " + start +
", before = " + before + ", count = " + count) ;
}
@Override
public void afterTextChanged ( Editable s) {
Log . d ( TAG, "afterTextChanged: " + s) ;
}
} ;
et. addTextChangedListener ( watcher) ;
27.Activity+viewPage+Fragment+RadioButton实现左右滑动切换页面
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
xmlns: tools= "http://schemas.android.com/tools"
android: id= "@+id/home15"
android: layout_width= "match_parent"
android: layout_height= "match_parent"
android: orientation= "vertical"
android: background= "@drawable/bjt"
tools: context= ".fragment.MainActivity" >
< android. support. v4. view. ViewPager
android: id= "@+id/main_ViewPager"
android: layout_width= "match_parent"
android: layout_height= "0dp"
android: layout_weight= "1" >
< / android. support. v4. view. ViewPager>
< LinearLayout
android: id= "@+id/root1"
android: layout_width= "match_parent"
android: layout_height= "45dp"
android: layout_gravity= "center"
android: gravity= "center"
android: visibility= "gone"
android: background= "#ffffff"
android: orientation= "vertical" >
< pl. droidsonroids. gif. GifImageView
android: layout_width= "130dp"
android: layout_height= "160dp"
android: background= "@drawable/dtz1" / >
< / LinearLayout >
< RadioGroup
android: id= "@+id/main_tab_RadioGroup"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: layout_alignParentBottom= "true"
android: background= "#000000"
android: alpha= "0.9"
android: orientation= "horizontal"
android: padding= "3dp" >
< RadioButton
android: id= "@+id/radio_home"
android: layout_width= "10dp"
android: layout_height= "40dp"
android: layout_weight= "1"
android: width= "100dp"
android: height= "100dp"
android: background= "@null"
android: button= "@null"
android: gravity= "center"
android: textStyle= "bold"
android: text= "首页"
android: textColor= "#ffffff" / >
< RadioButton
android: id= "@+id/radio_square"
android: layout_width= "10dp"
android: layout_height= "40dp"
android: layout_weight= "1"
android: width= "100dp"
android: height= "100dp"
android: background= "@null"
android: button= "@null"
android: gravity= "center"
android: text= "短视频"
android: textStyle= "bold"
android: textColor= "#ffffff" / >
< RadioButton
android: id= "@+id/radio_information"
android: layout_width= "10dp"
android: layout_height= "40dp"
android: layout_weight= "1"
android: width= "100dp"
android: height= "100dp"
android: background= "@null"
android: button= "@null"
android: gravity= "center"
android: text= "学习中心"
android: textColor= "#ffffff"
android: textStyle= "bold" / >
< RadioButton
android: id= "@+id/radio_me"
android: layout_width= "10dp"
android: layout_height= "40dp"
android: layout_weight= "1"
android: width= "100dp"
android: height= "100dp"
android: background= "@null"
android: button= "@null"
android: gravity= "center"
android: text= "我的"
android: textStyle= "bold"
android: textColor= "#ffffff" / >
< / RadioGroup >
< / LinearLayout >
public class TwoFragment extends Fragment {
public View onCreateView ( LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater. inflate ( R . layout. activity_login, container, false ) ;
return view;
}
}
public class MainActivity extends FragmentActivity implements OnCheckedChangeListener {
private ViewPager main_viewPager;
private RadioGroup main_tab_RadioGroup;
private RadioButton radio_home, radio_shopcar,
radio_sort, radio_me, radio_search;
private ArrayList < Fragment > fragmentList;
int current = 0 ;
@Override
public void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
if ( ( getIntent ( ) . getFlags ( ) & Intent . FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0 )
{
finish ( ) ;
return ;
}
setContentView ( R . layout. activity_login2) ;
if ( Build . VERSION. SDK_INT >= 21 ) {
View decorView = getWindow ( ) . getDecorView ( ) ;
decorView. setSystemUiVisibility ( View . SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View . SYSTEM_UI_FLAG_LAYOUT_STABLE) ;
getWindow ( ) . setStatusBarColor ( Color . TRANSPARENT) ;
int ui = decorView. getSystemUiVisibility ( ) ;
ui |= View . SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
decorView. setSystemUiVisibility ( ui) ;
}
InitView ( ) ;
InitViewPager ( ) ;
}
public void InitView ( ) {
main_tab_RadioGroup = ( RadioGroup ) findViewById ( R . id. main_tab_RadioGroup) ;
radio_home = ( RadioButton ) findViewById ( R . id. radio_home) ;
radio_shopcar = ( RadioButton ) findViewById ( R . id. radio_square) ;
radio_sort = ( RadioButton ) findViewById ( R . id. radio_information) ;
radio_me = ( RadioButton ) findViewById ( R . id. radio_me) ;
main_tab_RadioGroup. setOnCheckedChangeListener ( this ) ;
}
public void InitViewPager ( ) {
main_viewPager = ( ViewPager ) findViewById ( R . id. main_ViewPager) ;
fragmentList = new ArrayList < Fragment > ( ) ;
Fragment learnFragment = new TwoFragment ( ) ;
Fragment squareFragment = new SquareFragment ( ) ;
Fragment informationFragment = new InformationFragment ( ) ;
Fragment meFragment = new MeFragment ( ) ;
fragmentList. add ( squareFragment) ;
fragmentList. add ( learnFragment) ;
fragmentList. add ( informationFragment) ;
fragmentList. add ( meFragment) ;
main_viewPager. setAdapter ( new MyAdapter ( getSupportFragmentManager ( ) , fragmentList) ) ;
main_viewPager. setCurrentItem ( 0 ) ;
main_viewPager. addOnPageChangeListener ( new MyListner ( ) ) ;
}
public class MyAdapter extends FragmentPagerAdapter {
ArrayList < Fragment > list;
public MyAdapter ( FragmentManager fm, ArrayList < Fragment > list) {
super ( fm) ;
this . list = list;
}
@Override
public Fragment getItem ( int arg0) {
return list. get ( arg0) ;
}
@Override
public int getCount ( ) {
return list. size ( ) ;
}
}
public class MyListner implements ViewPager. OnPageChangeListener {
@Override
public void onPageScrollStateChanged ( int arg0) {
}
@Override
public void onPageScrolled ( int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected ( int arg0) {
int current = main_viewPager. getCurrentItem ( ) ;
switch ( current) {
case 0 :
main_tab_RadioGroup. check ( R . id. radio_home) ;
break ;
case 1 :
main_tab_RadioGroup. check ( R . id. radio_square) ;
break ;
case 2 :
main_tab_RadioGroup. check ( R . id. radio_information) ;
break ;
case 3 :
main_tab_RadioGroup. check ( R . id. radio_me) ;
break ;
}
}
}
@Override
public void onCheckedChanged ( RadioGroup radioGroup, int checkId) {
switch ( checkId) {
case R . id. radio_home:
current = 0 ;
break ;
case R . id. radio_square:
current = 1 ;
break ;
case R . id. radio_information:
current = 2 ;
break ;
case R . id. radio_me:
current = 3 ;
break ;
}
if ( main_viewPager. getCurrentItem ( ) != current) {
main_viewPager. setCurrentItem ( current) ;
}
}
protected void onStart ( ) {
super . onStart ( ) ;
}
}
28.估值器-贝塞尔曲线-控件封装-抖音点赞特效
import android. animation. TypeEvaluator ;
import android. graphics. PointF ;
public class BasEvaluator implements TypeEvaluator < PointF > {
private PointF p1;
private PointF p2;
public BasEvaluator ( PointF p1, PointF p2) {
super ( ) ;
this . p1 = p1;
this . p2 = p2;
}
@Override
public PointF evaluate ( float fraction, PointF p0, PointF p3) {
PointF pointf = new PointF ( ) ;
pointf. x = p0. x * ( 1 - fraction) * ( 1 - fraction ) * ( 1 - fraction)
+ 3 * p1. x * fraction * ( 1 - fraction ) * ( 1 - fraction )
+ 3 * p2. x * fraction * fraction * ( 1 - fraction )
+ p3. x* fraction * fraction * fraction ;
pointf. y = p0. y * ( 1 - fraction ) * ( 1 - fraction ) * ( 1 - fraction )
+ 3 * p1. y * fraction * ( 1 - fraction ) * ( 1 - fraction )
+ 3 * p2. y * fraction * fraction * ( 1 - fraction )
+ p3. y* fraction * fraction * fraction ;
return pointf;
}
}
import android. animation. Animator ;
import android. animation. AnimatorListenerAdapter ;
import android. animation. AnimatorSet ;
import android. animation. ObjectAnimator ;
import android. animation. ValueAnimator ;
import android. content. Context ;
import android. graphics. PointF ;
import android. graphics. drawable. Drawable ;
import android. util. AttributeSet ;
import android. view. animation. AccelerateDecelerateInterpolator ;
import android. view. animation. AccelerateInterpolator ;
import android. view. animation. DecelerateInterpolator ;
import android. view. animation. Interpolator ;
import android. view. animation. LinearInterpolator ;
import android. widget. ImageView ;
import android. widget. RelativeLayout ;
import com. example. amusic. R ;
import java. util. Random ;
public class Love_dy extends RelativeLayout {
private int a2= 8 ;
private Context context;
private LayoutParams params;
private Drawable [ ] icons = new Drawable [ a2] ;
private Interpolator [ ] interpolators = new Interpolator [ 4 ] ;
private int mWidth;
private int mHeight;
private int [ ] ImaResources = {
R . drawable. caomei, R . drawable. qiezi, R . drawable. heart_red, R . drawable. xigua, R . drawable. tuzi, R . drawable. tuzi,
R . drawable. hua, R . drawable. qiezi} ;
public Love_dy ( Context context, AttributeSet attrs) {
super ( context, attrs) ;
this . context = context;
initView ( ) ;
}
private void initView ( ) {
for ( int i= 0 ; i< a2; i++ ) {
icons[ i] = getResources ( ) . getDrawable ( ImaResources [ i] ) ;
}
interpolators[ 0 ] = new AccelerateDecelerateInterpolator ( ) ;
interpolators[ 1 ] = new AccelerateInterpolator ( ) ;
interpolators[ 2 ] = new DecelerateInterpolator ( ) ;
interpolators[ 3 ] = new LinearInterpolator ( ) ;
}
public void addLoveView ( float x, float y) {
if ( x < 100 ) {
x = 101 ;
}
if ( y < 100 ) {
y = 101 ;
}
mWidth = ( int ) ( x) ;
mHeight = ( int ) ( y) ;
final ImageView iv = new ImageView ( context) ;
params = new LayoutParams ( 200 , 200 ) ;
iv. setLayoutParams ( params) ;
iv. setImageDrawable ( icons[ new Random ( ) . nextInt ( a2) ] ) ;
addView ( iv) ;
AnimatorSet set = getAnimatorSet ( iv) ;
set. start ( ) ;
set. addListener ( new AnimatorListenerAdapter ( ) {
@Override
public void onAnimationEnd ( Animator animation) {
super . onAnimationEnd ( animation) ;
removeView ( iv) ;
}
} ) ;
}
private AnimatorSet getAnimatorSet ( ImageView iv) {
ObjectAnimator alpha = ObjectAnimator . ofFloat ( iv, "alpha" , 0.3f , 1f ) ;
ObjectAnimator scaleX = ObjectAnimator . ofFloat ( iv, "scaleX" , 0.2f , 1f ) ;
ObjectAnimator scaleY = ObjectAnimator . ofFloat ( iv, "scaleY" , 0.2f , 1f ) ;
AnimatorSet set = new AnimatorSet ( ) ;
set. playTogether ( alpha, scaleX, scaleY) ;
set. setDuration ( 2000 ) ;
ValueAnimator bzier = getBzierAnimator ( iv) ;
AnimatorSet set2 = new AnimatorSet ( ) ;
set2. playTogether ( set, bzier) ;
set2. setTarget ( iv) ;
return set2;
}
private ValueAnimator getBzierAnimator ( final ImageView iv) {
PointF [ ] PointFs = getPointFs ( iv) ;
BasEvaluator evaluator = new BasEvaluator ( PointFs [ 1 ] , PointFs [ 2 ] ) ;
ValueAnimator valueAnim = ValueAnimator . ofObject ( evaluator, PointFs [ 0 ] , PointFs [ 3 ] ) ;
valueAnim. addUpdateListener ( new ValueAnimator. AnimatorUpdateListener ( ) {
@Override
public void onAnimationUpdate ( ValueAnimator animation) {
PointF p = ( PointF ) animation. getAnimatedValue ( ) ;
iv. setX ( p. x) ;
iv. setY ( p. y) ;
iv. setAlpha ( 1 - animation. getAnimatedFraction ( ) ) ;
}
} ) ;
valueAnim. setTarget ( iv) ;
valueAnim. setDuration ( 2000 ) ;
valueAnim. setInterpolator ( interpolators[ new Random ( ) . nextInt ( 4 ) ] ) ;
return valueAnim;
}
private PointF [ ] getPointFs ( ImageView iv) {
PointF [ ] PointFs = new PointF [ 4 ] ;
PointFs [ 0 ] = new PointF ( ) ;
PointFs [ 0 ] . x = ( ( int ) mWidth) ;
PointFs [ 0 ] . y = mHeight;
PointFs [ 1 ] = new PointF ( ) ;
PointFs [ 1 ] . x = new Random ( ) . nextInt ( mWidth) ;
PointFs [ 1 ] . y = new Random ( ) . nextInt ( mHeight / 2 ) + mHeight / 2 + params. height;
PointFs [ 2 ] = new PointF ( ) ;
PointFs [ 2 ] . x = new Random ( ) . nextInt ( mWidth) ;
PointFs [ 2 ] . y = new Random ( ) . nextInt ( mHeight / 2 ) ;
PointFs [ 3 ] = new PointF ( ) ;
PointFs [ 3 ] . x = new Random ( ) . nextInt ( mWidth) ;
PointFs [ 3 ] . y = 0 ;
return PointFs ;
}
}
import android. content. Context ;
import android. view. GestureDetector. SimpleOnGestureListener ;
import android. view. MotionEvent ;
import android. widget. Toast ;
public class MyGestureListener extends SimpleOnGestureListener {
private Context mContext;
MyGestureListener ( Context context) {
mContext = context;
}
@Override
public boolean onDown ( MotionEvent e) {
Toast . makeText ( mContext, "DOWN " + e. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
return false ;
}
@Override
public void onShowPress ( MotionEvent e) {
Toast . makeText ( mContext, "SHOW " + e. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
}
@Override
public boolean onSingleTapUp ( MotionEvent e) {
Toast . makeText ( mContext, "SINGLE UP " + e. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
return false ;
}
@Override
public boolean onScroll ( MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Toast . makeText ( mContext, "SCROLL " + e2. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
return false ;
}
@Override
public void onLongPress ( MotionEvent e) {
Toast . makeText ( mContext, "LONG " + e. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
}
@Override
public boolean onFling ( MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Toast . makeText ( mContext, "FLING " + e2. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
return false ;
}
@Override
public boolean onDoubleTap ( MotionEvent e) {
Toast . makeText ( mContext, "DOUBLE " + e. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
return false ;
}
@Override
public boolean onDoubleTapEvent ( MotionEvent e) {
Toast . makeText ( mContext, "DOUBLE EVENT " + e. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
return false ;
}
@Override
public boolean onSingleTapConfirmed ( MotionEvent e) {
Toast . makeText ( mContext, "SINGLE CONF " + e. getAction ( ) , Toast . LENGTH_SHORT) . show ( ) ;
return false ;
}
}