BaseApplication
public class BaseApplication extends Application{
public static BaseApplication instance;
public static BaseApplication getInstance() {
return instance;
}
@Override
public void onCreate() {
super.onCreate();
instance = this;
FileUtils.CreateDir();//创建错误日志文件夹
if (CrashConfig.HAVE_LOG) {
CrashHandler crashHandler = CrashHandler.getInstance();
crashHandler.init(this.getApplicationContext());
}
boolean b = FileUtils.checkFilePathExists(FileUtils.SDPATH);
StringBuffer buffer = new StringBuffer();
buffer.append("是否会生成错误日志:"+(CrashConfig.HAVE_LOG+""))
.append("\n\n")
.append("当前编译模式:")
.append(BuildConfig.DEBUG ? "debug模式" : "release模式")
.append("\n\n")
.append("存放错误日志文件夹是否存在:" + b)
.append("\n\n")
.append("存放错误日志文件夹物理路径:")
.append("\n\n")
.append(FileUtils.file.getAbsolutePath());
}
********************************************
public class CrashConfig {
public static final boolean DEBUG = BuildConfig.DEBUG;
public static final boolean HAVE_LOG = DEBUG ? true : false;//debug产生错误日志
// public static final String HAVE_LOG = DEBUG ? false : true;//release产生错误日志
}
--------------------------------------------------------------
public class CrashHandler implements Thread.UncaughtExceptionHandler{
public static final String TAG = "CrashHandler";
//系统默认的UncaughtException处理类
private Thread.UncaughtExceptionHandler mDefaultHandler;
//CrashHandler实例
private static CrashHandler INSTANCE = new CrashHandler();
//程序的Context对象
private Context mContext;
//用于格式化日期,作为日志文件名的一部分
private DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
/**
* 保证只有一个CrashHandler实例
*/
private CrashHandler() {
}
/**
* 获取CrashHandler实例 ,单例模式
*/
public static CrashHandler getInstance() {
return INSTANCE;
}
/**
* 初始化
*
* @param context
*/
public void init(Context context) {
mContext = context;
//获取系统默认的UncaughtException处理器
mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
//设置该CrashHandler为程序的默认处理器
Thread.setDefaultUncaughtExceptionHandler(this);
}
/**
* 当UncaughtException发生时会转入该函数来处理
*/
@Override
public void uncaughtException(Thread thread, Throwable ex) {
if (!handleException(ex) && mDefaultHandler != null) {
//如果用户没有处理则让系统默认的异常处理器来处理
mDefaultHandler.uncaughtException(thread, ex);
} else {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
Log.e(TAG, "error : ", e);
}
//退出程序
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(1);
}
}
/**
* 自定义错误处理,收集错误信息 发送错误报告等操作均在此完成.
*
* @param ex
* @return true:如果处理了该异常信息;否则返回false.
*/
private boolean handleException(final Throwable ex) {
if (ex == null) {
return false;
}
final String strhh = saveCrashInfo2File(ex);
Log.e(TAG, strhh);
//使用Toast来显示异常信息
new Thread() {
@Override
public void run() {
Looper.prepare();
Toast.makeText(mContext, strhh, Toast.LENGTH_LONG).show();
// Toast.makeText(mContext, "很抱歉,程序出现异常,即将退出.", Toast.LENGTH_LONG).show();
Looper.loop();
}
}.start();
//收集设备参数信息,保存日志文件
writeFileSdcardFile(FileUtils.SDPATH, "Crash_" + System.currentTimeMillis() + ".txt", saveCrashInfo2File(ex), ex.getMessage());
return true;
}
/**
* 收集设备信息与错误日志
*
* @param e
*/
public String saveCrashInfo2File(Throwable e) {
StringBuilder sb = new StringBuilder();
sb.append("生产厂商:\n");
sb.append(Build.MANUFACTURER).append("\n\n");
sb.append("手机型号:\n");
sb.append(Build.MODEL).append("\n\n");
sb.append("系统版本:\n");
sb.append(Build.VERSION.RELEASE).append("\n\n");
sb.append("异常时间:\n");
sb.append(formatter.format(new Date())).append("\n\n");
sb.append("异常类型:\n");
sb.append(e.getClass().getName()).append("\n\n");
sb.append("异常信息:\n");
sb.append(e.getMessage()).append("\n\n");
sb.append("异常堆栈:\n");
Writer writer = new StringWriter();
PrintWriter printWriter = new PrintWriter(writer);
e.printStackTrace(printWriter);
Throwable cause = e.getCause();
while (cause != null) {
cause.printStackTrace(printWriter);
cause = cause.getCause();
}
printWriter.close();
String result = writer.toString();
sb.append(result);
return sb.toString();
}
/**
* 保存错误信息到文件中
*
* @param path
* @param fileName 文件名
* @param write_str 错误日志
* @param ex 错误信息
*/
public void writeFileSdcardFile(String path, String fileName, String write_str, String ex) {
if (!FileUtils.file.exists()) {
FileUtils.CreateDir();
}
try {
FileOutputStream fout = new FileOutputStream(path + fileName);
byte[] bytes = write_str.getBytes();
fout.write(bytes);
fout.close();
Log.e(TAG, "保存成功" + path + fileName);
//此地做上传错误日志代码
// uploadLogFile(new File(path + fileName), ex);
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "保存失败");
}
}
**************************************************************
public class FileUtils {
public static String SDPATH = Environment.getExternalStorageDirectory() + "/ACrash/";//文件夹路径
public static File file = new File(SDPATH);
public static void CreateDir() {
if (!file.exists()) {//创建文件夹
file.mkdirs();
}
}
/**
* 检查路径是否存在
*
* @param path
* @return
*/
public static boolean checkFilePathExists(String path) {
return new File(path).exists();
}
}
-----------------------------------------
Mainactivity
private ViewPager vp;
private RadioButton rb_1;
private RadioButton rb_2;
private RadioButton rb_3;
private RadioGroup rg;
private List<Fragment> Flist = new ArrayList<>();
private Title_View title;
private TextView rb_1_text;
private TextView rb_2_text;
private TextView rb_3_text;
private LinearLayout text_ll;
\\\\\\\\\
initView();
MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
vp.setAdapter(myFragmentPagerAdapter);
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
switch (checkedId) {
case R.id.rb_1:
System.out.println("===>1");
vp.setCurrentItem(0);
title.setTitle(rb_1_text.getText().toString().trim());
break;
case R.id.rb_2:
vp.setCurrentItem(1);
title.setTitle(rb_2_text.getText().toString().trim());
break;
case R.id.rb_3:
System.out.println("===>3");
vp.setCurrentItem(2);
title.setTitle(rb_3_text.getText().toString().trim());
break;
}
}
});
vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
switch (position){
case 0:
rg.check(R.id.rb_1);
break;
case 1:
rg.check(R.id.rb_2);
break;
case 2:
rg.check(R.id.rb_3);
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initView() {
vp = (ViewPager) findViewById(R.id.vp);
rb_1 = (RadioButton) findViewById(R.id.rb_1);
rb_2 = (RadioButton) findViewById(R.id.rb_2);
rb_3 = (RadioButton) findViewById(R.id.rb_3);
rg = (RadioGroup) findViewById(R.id.rg);
Flist.add(new Fragment_One());
Flist.add(new Fragment_One());
Flist.add(new Fragment_One());
title = (Title_View) findViewById(R.id.title);
rb_1_text = (TextView) findViewById(R.id.rb_1_text);
rb_2_text = (TextView) findViewById(R.id.rb_2_text);
rb_3_text = (TextView) findViewById(R.id.rb_3_text);
text_ll = (LinearLayout) findViewById(R.id.text_ll);
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return Flist.get(position);
}
@Override
public int getCount() {
return Flist.size();
}
}
///
public class Title_View extends RelativeLayout {
private TextView title_text;
private ImageView title_img_left;
public Title_View(Context context) {
super(context);
initview(context);
}
private void initview(Context context) {
LayoutInflater.from(context).inflate(R.layout.title_view,
this);
title_text = (TextView) findViewById(R.id.title_text);
title_img_left= (ImageView) findViewById(R.id.title_img_left);
}
//设置左侧imageview点击事件
public void setLeftlistener(OnClickListener listener) {
findViewById(R.id.title_img_left).setOnClickListener(listener);
}
//设置右侧imageview点击事件
public void setRightlistener(OnClickListener listener) {
findViewById(R.id.title_img_right).setOnClickListener(listener);
}
//设置标题
public void setTitle(String text) {
title_text.setText(text);
}
public Title_View(Context context, AttributeSet attrs) {
super(context, attrs);
initview(context);
}
public Title_View(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
-----------------------------------------------------------------------
public class Fragment_One extends Fragment {
private View view;
private ListView one_list;
private List<String> list = new ArrayList<>();
//设置动画时间
int duration = 1000;
private boolean isCheck = true;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_one, null);
initView(view);
MyAdapter adapter = new MyAdapter();
one_list.setAdapter(adapter);
return view;
}
//初始化数据
private void initView(View view) {
one_list = (ListView) view.findViewById(R.id.one_list);
for (int i = 0; i < 10; i++) {
list.add(i + "");
}
}
//适配器
private class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(getActivity(), R.layout.fragment_one_item, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//点击按钮开始动画
holder.jia.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ObjectAnimator animator1 = null;
ObjectAnimator animator2 = null;
ObjectAnimator animator3 = null;
ObjectAnimator animator4 = null;
if (isCheck) {
//加号旋转
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
//平移
PropertyValuesHolder p21 = PropertyValuesHolder.ofFloat("TranslationX", 0f, -58f);
PropertyValuesHolder p22 = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
PropertyValuesHolder p23 = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
PropertyValuesHolder p31 = PropertyValuesHolder.ofFloat("TranslationX", 0f, -116f);
PropertyValuesHolder p41 = PropertyValuesHolder.ofFloat("TranslationX", 0f, -174f);
animator1 = ObjectAnimator.ofPropertyValuesHolder(holder.jia, p1).setDuration(duration);
animator2 = ObjectAnimator.ofPropertyValuesHolder(holder.dun, p21,p22,p23).setDuration(duration);
animator3 = ObjectAnimator.ofPropertyValuesHolder(holder.lianjie, p31,p22,p23).setDuration(duration);
animator4 = ObjectAnimator.ofPropertyValuesHolder(holder.yuanjian, p41,p22,p23).setDuration(duration);
isCheck=false;
} else {
//加号旋转
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
//平移
PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("TranslationX", -58f, 0f);
PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("TranslationX", -116f, 0f);
PropertyValuesHolder p4 = PropertyValuesHolder.ofFloat("TranslationX", -174f, 0f);
PropertyValuesHolder p22 = PropertyValuesHolder.ofFloat("alpha", 1f, 0f);
PropertyValuesHolder p23 = PropertyValuesHolder.ofFloat("rotation", 0f, 360f);
animator1 = ObjectAnimator.ofPropertyValuesHolder(holder.jia, p1).setDuration(duration);
animator2 = ObjectAnimator.ofPropertyValuesHolder(holder.dun, p22,p23,p2).setDuration(duration);
animator3 = ObjectAnimator.ofPropertyValuesHolder(holder.lianjie, p22,p23,p3).setDuration(duration);
animator4 = ObjectAnimator.ofPropertyValuesHolder(holder.yuanjian, p22,p23,p4).setDuration(duration);
isCheck=true;
}
animator1.setInterpolator(new BounceInterpolator());
animator2.setInterpolator(new BounceInterpolator());
animator3.setInterpolator(new BounceInterpolator());
animator4.setInterpolator(new BounceInterpolator());
//启动动画
animator1.start();
animator2.start();
animator3.start();
animator4.start();
animator1.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
if(isCheck){
holder.jia.setImageResource(R.drawable.icon_open);
}else {
holder.jia.setImageResource(R.drawable.icon_packup);
}
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
});
return convertView;
}
}
public static class ViewHolder {
public View rootView;
public ImageView yuanjian;
public ImageView lianjie;
public ImageView dun;
public ImageView jia;
public ViewHolder(View rootView) {
this.rootView = rootView;
this.yuanjian = (ImageView) rootView.findViewById(R.id.yuanjian);
this.lianjie = (ImageView) rootView.findViewById(R.id.lianjie);
this.dun = (ImageView) rootView.findViewById(R.id.dun);
this.jia = (ImageView) rootView.findViewById(R.id.jia);
}
}
}
---------------------------------------
public class Fragment_Tow extends Fragment{
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view=inflater.inflate(R.layout.fragment_tow,null);
return view;
}
}
布局
main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="cheer.zhaolantian1507a20171023.MainActivity">
<cheer.zhaolantian1507a20171023.view.Title_View
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="66dp"></cheer.zhaolantian1507a20171023.view.Title_View>
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/rg"
android:layout_below="@+id/title">
</android.support.v4.view.ViewPager>
<RadioGroup
android:layout_above="@+id/text_ll"
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="66dp"
android:orientation="horizontal">
<RadioButton
android:checked="true"
android:id="@+id/rb_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/rb_bg"
android:button="@null"
/>
<RadioButton
android:id="@+id/rb_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/rb_bg"
android:button="@null"
/>
<RadioButton
android:id="@+id/rb_3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/rb_bg"
android:button="@null"
/>
</RadioGroup>
<LinearLayout
android:id="@+id/text_ll"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/rb_1_text"
android:text="推荐"
android:layout_weight="1"
android:layout_width="0dp"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/rb_2_text"
android:text="段子"
android:layout_weight="1"
android:layout_width="0dp"
android:gravity="center"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/rb_3_text"
android:text="视频"
android:layout_weight="1"
android:layout_width="0dp"
android:gravity="center"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
fragmentone
<ListView
android:id="@+id/one_list"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
titleView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="42dp"
android:background="#03A9F5"
>
<ImageView
android:layout_marginLeft="12dp"
android:id="@+id/title_img_left"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="@drawable/ic_launcher"
android:layout_alignParentLeft="true"
/>
<TextView
android:layout_toLeftOf="@+id/title_img_right"
android:layout_toRightOf="@+id/title_img_left"
android:id="@+id/title_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="段子"
android:textColor="#fff"
android:textSize="24dp"
/>
<ImageView
android:id="@+id/title_img_right"
android:layout_width="42dp"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher_round"
android:layout_alignParentRight="true"
android:layout_marginRight="12dp"
/>
</RelativeLayout>