您的位置:首页 > 其它

智慧xxx项目记录(一)splash闪屏和Gridle引导界面

2016-10-17 17:38 183 查看
遇到问题:

一、androidstudio使用TortoiseSVN

先打开TortoiseSVN的安装包,选择修复,安装有command-line功能;

二、Gridle引导图片存放在drawable文件夹下,会导致三星S6等大屏手机OOM,解决方法为:

1、将图片存放在mipmap目录下;

2、使用的时候,请调用一下对应的方法:

setBackgroundResource和android:background→setBackgroundResource(0);

setBackgroundDrawable(background)→setBackgroundDrawable(null);

setBackground(background)→setBackground(null);

然后在onDestory中调用System.gc();

原文地址为:http://www.cnblogs.com/bianmajiang/p/4044076.html

三、在onctreate()方法中直接计算两个圆点之间的距离之后并打印

mPointDistance=ll_container.getChildAt(1).getLeft()-ll_container.getChildAt(0).getLeft();//连个圆点之间的距离

System.out.println(mPointDistance);

会发现打印结果为0,导致原因:

Activity在启动执行oncreate()方法后,才会执行measure(测量)–>layout(确定位置)–>drawable(绘制);

直接放在oncreate()位置时,oncreate()方法未执行完毕,当然此时的布局控件的位置都无法确定,所以打印出的两个圆点距离为0

解决方法:

添加系统layout方法结束的监听(使用getViewTreeObserver()视图树观察者监听),等到layout执行结束确定位置后再计算两个圆点之间的距离

记录:

splash闪屏:

startappanim = (LinearLayout) findViewById(R.id.startappanim);

//旋转动画,图片基于自身中心点旋转360度,时间为1秒
RotateAnimation rotateAnim = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnim.setDuration(1000);
rotateAnim.setFillAfter(true);//保持动画的最后界面

//缩放动画,图片基于自身中心点从0到1缩放,时间为1秒
ScaleAnimation scaleAnim = new ScaleAnimation(0, 1, 0, 1, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnim.setDuration(1000);
rotateAnim.setFillAfter(true);//保持动画的最后界面

//渐变动画,图片颜色从0到1渐变,时间为2秒
AlphaAnimation alphaAnim = new AlphaAnimation(0, 1);
alphaAnim.setDuration(2000);
rotateAnim.setFillAfter(true);//保持动画的最后界面

//图片动画集合
AnimationSet animSet = new AnimationSet(true);
animSet.addAnimation(rotateAnim);
animSet.addAnimation(scaleAnim);
animSet.addAnimation(alphaAnim);

//开启动画
startappanim.startAnimation(animSet);

//动画动作监听
animSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {

}

@Override
//当动画播放结束后进入新手引导页(第一次开启APP)或者进入主界面
public void onAnimationEnd(Animation animation) {
Boolean is_first = PreUtils.getBoolean(getApplicationContext(), "is_first", true);
Intent startintent;
if (is_first) {
//当is_first为真时,进入新手引导页
startintent = new Intent(getApplicationContext(), GuideActivity.class);
} else {
//当is_first为假时,进入主界面
startintent = new Intent(getApplicationContext(), MainActivity.class);
}
//开启界面
startActivity(startintent);
//关闭当前动画界面,系统回收资源
finish();

}

@Override
public void onAnimationRepeat(Animation animation) {

}
});


制作Utils工具类,用于判断是否第一次登录等信息:

/**
* SharedPreferences的封装
*
*/
public class PreUtils {

public static Boolean getBoolean(Context context, String Key, Boolean defaultvalue) {
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
return sp.getBoolean(Key, defaultvalue);
}

public static void setBoolean(Context context, String Key, Boolean defaultvalue) {
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
sp.edit().putBoolean(Key,defaultvalue).apply();
}

public static String getString(Context context, String Key, String value) {
SharedPreferences sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
return sp.getString(Key, value);
}

public static void setString(Context context, String Key, String value) {
SharedPreferences sp = context.getSharedPreferences("config",  Context.MODE_PRIVATE);
sp.edit().putString(Key, value).apply();
}

public static int getInt(Context context, String Key, int value) {
SharedPreferences sp = context.getSharedPreferences("config",  Context.MODE_PRIVATE);
return sp.getInt(Key, value);
}

public static void setInt(Context context, String Key, int value) {
SharedPreferences sp = context.getSharedPreferences("config",  Context.MODE_PRIVATE);
sp.edit().putInt(Key, value).apply();
}
}


Gridle引导界面(使用v4的ViewPager,小红点随ViewPager滑动而移动):

计算两个圆点之间的距离(两个圆点之间的间距+小红点的直径)

计算方法:第二个圆点的left值-第一个圆点的left值

在onctreate()方法中直接计算两个圆点之间的距离之后并打印

mPointDistance=ll_container.getChildAt(1).getLeft()-ll_container.getChildAt(0).getLeft();//连个圆点之间的距离

System.out.println(mPointDistance);

会发现打印结果为0,导致原因:

Activity在启动执行oncreate()方法后,才会执行measure(测量)–>layout(确定位置)–>drawable(绘制);

直接放在oncreate()位置时,oncreate()方法未执行完毕,当然此时的布局控件的位置都无法确定,所以打印出的两个圆点距离为0

解决方法:

添加系统layout方法结束的监听(使用getViewTreeObserver()视图树观察者监听),等到layout执行结束确定位置后再计算两个圆点之间的距离

public class GuideActivity extends Activity implements View.OnClickListener {
private Button btn_start;
private ViewPager mviewPager;
private LinearLayout ll_container;
private ImageView iv_red_point;
private int mPointDistance;//两个圆点之间的距离

//图片存放在drawable下,大屏手机会有OOM问题
private int[] mImageIds = new int[]{R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};
private ArrayList<ImageView> mImageViewList;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//加载布局文件
setContentView(R.layout.activity_guide);

btn_start= (Button) findViewById(R.id.btn_start);
btn_start.setOnClickListener(this);

ll_container = (LinearLayout) findViewById(R.id.ll_container);

initData();

iv_red_point = (ImageView) findViewById(R.id.iv_red_point); //小红点

mviewPager = (ViewPager) findViewById(R.id.vp_guide);  //ViewPager
//要在viewpager前先初始化数据,否则会有空指针
mviewPager.setAdapter(new Guideadater());//适配器

/*
//计算两个圆点之间的距离(两个圆点之间的间距+小红点的直径)
//计算方法:第二个圆点的left值-第一个圆点的left值
mPointDistance=ll_container.getChildAt(1).getLeft()-ll_container.getChildAt(0).getLeft();//连个圆点之间的距离
System.out.println(mPointDistance);
打印结果为0,原因:
Activity在启动执行oncreate方法后,才会执行measure(测量)-->layout(确定位置)-->drawable(绘制);
放在此位置时oncreate方法未执行完毕,所以控件的位置都无法确定,所以打印出的两个圆点距离为0
解决方法:
添加layout方法结束的监听(使用getViewTreeObserver()视图树观察者监听),等到layout执行结束确定位置后再计算两个圆点之间的距离
*/

mviewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//当页面在滑动过程中的回调
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//position:当前页面位置;positionOffset:移动偏移的百分比;positionOffsetPixels:移动;偏移的像素
//小红点移动距离=移动偏移百分比*(两个圆点间距+小红点直径即两个圆点距离左边距只差)
//更新小红点距离
int redleftmargin= (int) (mPointDistance*positionOffset)+position*mPointDistance;//计算当前滑动的左边距
RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) iv_red_point.getLayoutParams(); //获取小红点控件的布局
params.leftMargin=redleftmargin; //重新设置小红点布局参数的左边距
iv_red_point.setLayoutParams(params); //往小红点控件中加载布局参数
}

//某个页面被选中
@Override
public void onPageSelected(int position) {

if (position==mImageViewList.size()-1) {  //当滑动到最后一个引导页,按钮设为可见
btn_start.setVisibility(View.VISIBLE);
}
}

//页面状态发生变化的回调
@Override
public void onPageScrollStateChanged(int state) {

}
});

//使用getViewTreeObserver()监听系统的全局layout视图树是否结束
iv_red_point.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
//由于系统底层会进行多次重绘,所以在监听到一次之后就结束监听
iv_red_point.getViewTreeObserver().removeOnGlobalLayoutListener(this);
//计算两个圆点之间的距离(两个圆点之间的间距+小红点的直径)
//计算方法:第二个圆点的left值-第一个圆点的left值
mPointDistance=ll_container.getChildAt(1).getLeft()-ll_container.getChildAt(0).getLeft();//连个圆点之间的距离
System.out.println(mPointDistance);
}

});
}

//初始化数据(图片资源)
private void initData() {
mImageViewList = new ArrayList<ImageView>();
for (int i = 0; i < mImageIds.length; i++) {

ImageView mImageView = new ImageView(this);
mImageView.setBackgroundResource(mImageIds[i]);//设置背景图片,使图片能填充窗体
// mimageview.setImageResource(mImageIds[i]);//无法保证图片能够填充窗体
mImageViewList.add(mImageView);//将图片加载到数组列表中

//初始化灰色小圆点
ImageView point = new ImageView(this);
point.setImageResource(R.drawable.shape_point_gray);//设置图片(shape形状)
//初始化布局参数,宽高包裹内容,父控件是谁,就声明谁的布局参数
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);//宽高包裹内容

if (i > 0) { //从第二个小圆点开始设置左边距为40
params.leftMargin = 40;
}
point.setLayoutParams(params); //设置布局参数
ll_container.addView(point);  //往父容器中添加小圆点
}
}

/*
* 按钮开始体验的点击事件
* */
@Override
public void onClick(View v) {

PreUtils.setBoolean(this,"is_first",false);  //编辑SP,将第一次进入的值设为false
Intent intent=new Intent(this,MainActivity.class);
startActivity(intent);//开启意图,进入主页面
finish(); //关闭当前画面
}

private class Guideadater extends PagerAdapter {
//获取item长度,即图片的张数
@Override
public int getCount() {
return mImageViewList.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

//初始化item布局
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView view = mImageViewList.get(position);//获取当前位置的图片对象
container.addView(view);//将当前图片添加到ViewGroup中
return view;
}

//销毁item布局
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}

}
}


Grile.xml布局文件:

小圆点之间采用相对布局,下面为linearLayout,linearLayout里动态添加灰色小圆点;上面为ImageView的红色小圆点,由于两层都为居父容器居中,所以红色小圆点刚好覆盖灰色小圆点

<?xml version="1.0" encoding="utf-8"?>
<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="com.yhui.smartfujian.GuideActivity">

<android.support.v4.view.ViewPager
android:id="@+id/vp_guide"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="40dp"
android:visibility="invisible"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@drawable/btn_guide_selector"
android:textColor="@color/text_guide_selector"
android:layout_centerHorizontal="true"
android:text="开始体验"/>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"
android:layout_centerHorizontal="true"
>
<LinearLayout
android:id="@+id/ll_container"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
>
</LinearLayout>

<ImageView
android:id="@+id/iv_red_point"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shape_point_red"/>
</RelativeLayout>

</RelativeLayout>


shape_point_gray.xml灰色小圆点布局文件(放在drawable目录下):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#cccccc" />
<size
android:width="10dp"
android:height="10dp" />
</shape>


shape_point_red.xml红色小圆点布局文件(放在drawable目录下):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#ff0000" />
<size
android:width="10dp"
android:height="10dp" />
</shape>


btn_guide_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_red_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/button_red_normal"/>
</selector>


text_guide_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#000000" android:state_pressed="true"/>
<item android:color="#ffffff"/>
</selector>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: