Basics of Android App development: new features

Source: Internet
Author: User

Fragment
    • Purpose: Switch the interface in an activity, switch the interface only to switch the contents of fragment
    • The life cycle approach is consistent with activity and can be understood as an activity
    • Defining the layout file as a fragment display

      //此方法返回的View就会被显示在Fragment上@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,    Bundle savedInstanceState) {    // TODO Auto-generated method stub    //用布局文件填充成一个View对象,返回出去,那么就显示在Fragment上了    View v = inflater.inflate(R.layout.fragment01, null);    return v;     }
    • Display the fragment to the specified ViewGroup

      //把fragment显示至界面//new出fragment对象Fragment01 fg = new Fragment01();FragmentManager fm = getFragmentManager();//开启事务FragmentTransaction ft = fm.beginTransaction();//把fragment对象显示到指定资源id的组件里面ft.replace(R.id.fl, fg);ft.commit();
Life cycle
    • The old fragment object is destroyed when the fragment switch, and the new fragment object is created
Low version compatible
    • There are APIs in the Support-v4.jar package, which means that fragment can be run in the low version emulator
Animated frame Animations

A picture is constantly switched to create an animated effect

    • In the drawable directory, define the XML file, the child node is animation-list, where you define the picture to display and the length of time each picture is displayed

      <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">    <item android:drawable="@drawable/g1" android:duration="200" />    <item android:drawable="@drawable/g2" android:duration="200" />    <item android:drawable="@drawable/g3" android:duration="200" /></animation-list>
    • Play frame animations on the screen

      ImageView iv = (ImageView) findViewById(R.id.iv);//把动画文件设置为imageView的背景iv.setBackgroundResource(R.drawable.animations);AnimationDrawable ad = (AnimationDrawable) iv.getBackground();//播放动画        ad.start();
Motion Tweens
    • When the original form becomes a new form, the resulting animation is called a motion tween, in order to transition the deformation process.
    • Displacement, rotation, scaling, transparency
Displacement:
    • Parameter 10 refers to the start coordinate of X, but does not refer to the position of the X coordinate of the screen, but the real x + 10 of the ImageView.
    • Parameter 150 refers to the end coordinate of x, whose value is ImageView's true x + 150

      //创建为位移动画对象,设置动画的初始位置和结束位置TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
    • The starting position of the x-coordinate, if relative to yourself, is 0.5f, then the starting coordinates are true X + 0.5 * IV width
    • The end position of the X coordinate, if passed in 2, then the end coordinate is the width of the real x + 2 * IV
    • The start position of the y-coordinate, if passed in 0.5f, then the starting coordinates are true Y + 0.5 * IV height
    • The end position of the y-coordinate, if passed in 2, then the end coordinate is true y + 2 * IV height

      TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);
    • Settings related to animation playback

      //设置动画持续时间ta.setDuration(2000);//动画重复播放的次数ta.setRepeatCount(1);//动画重复播放的模式ta.setRepeatMode(Animation.REVERSE);//动画播放完毕后,组件停留在动画结束的位置上ta.setFillAfter(true);//播放动画iv.startAnimation(ta);
Scaling:
    • Parameter 0.1f indicates that the starting width of the animation is 0.1 times times the true width
    • Parameter 4 indicates that the end width of the animation is 4 times times the true width
    • The center point of the zoom is in the upper left corner of the IV

      ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
    • Parameters 0.1f and 4 have the same meaning as above
    • Change the center point of the zoom: Incoming two 0.5f, the type is relative to itself, these two parameters change the center point of the zoom
    • Center point x Coordinate = true x + 0.5 * IV width
    • Center point Y coordinate = true y + 0.5 * IV height

      Scaleanimation sa = new Scaleanimation (0.1f, 4, 0.1f, 4, animation.relativeto self, 0.5f, animation.relative to self, 0.5f);

Transparent:
    • 0 is fully transparent and 1 is completely opaque

      AlphaAnimation aa = new AlphaAnimation(0, 0.5f);
Rotating:
    • 20 indicates the angle of the IV at the beginning of the animation
    • 360 indicates the angle of the IV at the end of the animation
    • The center of the default rotation is in the upper left corner of IV

      RotateAnimation ra = new RotateAnimation(20, 360);
    • 20,360 of the meaning is the same as above
    • Specifies the center coordinate, relative to itself, the value is passed in 0.5, then the X coordinate of the center: true x + IV Width * 0.5
    • Y coordinate of Center point: true y + IV Height * 0.5

      RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
All animations once take off
    //创建动画集合    AnimationSet set = new AnimationSet(false);    //往集合中添加动画    set.addAnimation(aa);    set.addAnimation(sa);    set.addAnimation(ra);    iv.startAnimation(set);
Property animations
    • tweened animation, just an animation effect, the component is actually in the original position, XY did not change
Displacement:
    • The first parameter, target, specifies the component to display the animation
    • The second parameter, PropertyName, specifies which property of the component to change
    • The third parameter, values, is a mutable parameter, which is the new value given to the property
    • Incoming 0, representing x starting coordinates: current x + 0
    • Pass in 100, which represents the x endpoint coordinates: current x + 100

      //具有get、set方法的成员变量就称为属性ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;
Scaling:
    • The third parameter specifies the scale of the scale
    • 0.1 is starting from One-tenth of the original height.
    • 2 is twice times the end of the original height

      ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
Transparent:
    • Transparency, 0 is completely transparent, 1 is completely opaque

      ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
Rotating
    • rotation specifies that clockwise rotation
    • 20 is the starting angle
    • 270 is the end angle

      ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
    • Property specified as RotationX is inverted vertically
    • Property specified as RotationY is flipped horizontally

      ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);
Variable parameters
    • The third parameter variable parameter can be passed in multiple parameters, can be used to achieve back displacement (rotation, scaling, transparency)

      ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;
All animations once take off
    //创建动画师集合    AnimatorSet set = new AnimatorSet();    //设置要播放动画的组件    set.setTarget(bt);    //所有动画有先后顺序的播放    //set.playSequentially(oa, oa2, oa3, oa4);    //所有动画一起播放    set.playTogether(oa, oa2, oa3, oa4);    set.start();
dialog box OK Cancel dialog box
    • Create a Dialog builder object similar to Factory mode
    • AlertDialog.Builder builder = new Builder(this);
    • Set the title and body
    • builder.setTitle("警告");builder.setMessage("若练此功,必先自宫");
    • Set OK and Cancel buttons

      builder.setPositiveButton("现在自宫", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {        // TODO Auto-generated method stub        Toast.makeText(MainActivity.this, "恭喜你自宫成功,现在程序退出", 0).show();    }});builder.setNegativeButton("下次再说", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {        // TODO Auto-generated method stub        Toast.makeText(MainActivity.this, "若不自宫,一定不成功", 0).show();    }});
    • Using the Builder to create a dialog box object

      AlertDialog ad = builder.create();ad.show();
Single-selection dialog box
    AlertDialog.Builder builder = new Builder(this);    builder.setTitle("选择你的性别");
    • Define a radio option
    • final String[] items = new String[]{        "男", "女", "其他"};//-1表示没有默认选择//点击侦听的导包要注意别导错builder.setSingleChoiceItems(items, -1, new OnClickListener() {    //which表示点击的是哪一个选项    @Override    public void onClick(DialogInterface dialog, int which) {        Toast.makeText(MainActivity.this, "您选择了" + items[which], 0).show();        //对话框消失        dialog.dismiss();    }});builder.show();
Multi-Select dialog box
    AlertDialog.Builder builder = new Builder(this);    builder.setTitle("请选择你认为最帅的人");
    • The option to define multiple selections, because you can select multiple, so you need a Boolean array to record which options are selected
    • final String[] items = new String[]{        "赵帅哥",        "赵师哥",        "赵老师",        "侃哥"};//true表示对应位置的选项被选了final boolean[] checkedItems = new boolean[]{        true,        false,        false,        false,};builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {    //点击某个选项,如果该选项之前没被选择,那么此时isChecked的值为true    @Override    public void onClick(DialogInterface dialog, int which, boolean isChecked) {        checkedItems[which] = isChecked;    }});builder.setPositiveButton("确定", new OnClickListener() {    @Override    public void onClick(DialogInterface dialog, int which) {        StringBuffer sb = new StringBuffer();        for(int i = 0;i < items.length; i++){            sb.append(checkedItems[i] ? items[i] + " " : "");        }        Toast.makeText(MainActivity.this, sb.toString(), 0).show();    }});builder.show();
Progress Bar dialog box
    • The Progress Bar dialog box can be directly new

      final ProgressDialog pd = new ProgressDialog(this);
    • Sets the style of the progress bar, which can be rotated or a horizontal progress bar

      pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    • pd.setTitle("正在加载,请稍后...")//设置进度条进度值的最大值pd.setMax(200);pd.show();//开启子线程改变进度条的进度值Thread t = new Thread(){    @Override    public void run() {        // TODO Auto-generated method stub        super.run();        for (int i = 0; i < 200; i++) {            try {                sleep(30);            } catch (InterruptedException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            //改变进度值            pd.setProgress(i);        }        pd.dismiss();    }};t.start();

Basics of Android App development: new features

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.