Android to scroll up and down textview_android

Source: Internet
Author: User
Tags prev stub

A description
Here the important application class Autotextview, which is a custom class, inherits to Textswitcher, under the face of Autotextview class do a brief description:

1. The focus of this type of application is to set up two animations, setinanimation (...) and Setoutanimation (...), separation is the text into the animation and text exit animation;

2. class defines an external class-rotate3danimation, which is important to the implementation of text entry and exit animation, the outer class inherits to animation. Coincidentally, this happens to be seen in the Apidemo, custom animation I was first applied, the animation logic is in the void applytransformation (float interpolatedtime, transformation t), the code is very sharp, I on the original basis, changed a bit, to achieve the above effect,

Two code sections:
1.autotextview.java

Copy Code code as follows:

Package Com.example.animtextview;

Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Camera;
Import Android.graphics.Matrix;
Import Android.util.AttributeSet;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.animation.AccelerateInterpolator;
Import android.view.animation.Animation;
Import android.view.animation.Transformation;
Import Android.widget.TextSwitcher;
Import Android.widget.TextView;
Import Android.widget.ViewSwitcher;

public class Autotextview extends Textswitcher implements
viewswitcher.viewfactory {

private float mheight;
Private context Mcontext;
Minup,moutup separation constitutes the movement of the page down through the animation
Private Rotate3danimation Minup;
Private Rotate3danimation Moutup;

Mindown,moutdown separation constitutes the movement of the page down through the animation
Private Rotate3danimation Mindown;
Private Rotate3danimation Moutdown;

Public Autotextview {
This is (context, NULL);
TODO auto-generated Constructor stub
}

Public Autotextview (context context, AttributeSet Attrs) {
Super (context, attrs);
TODO auto-generated Constructor stub
TypedArray a = Context.obtainstyledattributes (Attrs, r.styleable.auto3d);
Mheight = A.getdimension (r.styleable.auto3d_textsize, 36);
A.recycle ();
Mcontext = context;
Init ();
}

private void init () {
TODO auto-generated Method Stub
Setfactory (this);
Minup = Createanim ( -90, 0, True, true);
Moutup = Createanim (0, N, False, True);
Mindown = Createanim (0, True, false);
Moutdown = Createanim (0, -90, false, false);
Textswitcher important for file switching, such as switching from text A to text B,
After Setinanimation (), a will execute inanimation,
After Setoutanimation (), B executes the outanimation
Setinanimation (Minup);
Setoutanimation (Moutup);
}

Private rotate3danimation Createanim (float start, float end, Boolean turnin, Boolean turnup) {
Final rotate3danimation rotation = new Rotate3danimation (start, End, Turnin, turnup);
Rotation.setduration (800);
Rotation.setfillafter (FALSE);
Rotation.setinterpolator (New Accelerateinterpolator ());
return rotation;
}

The TextView back here is the view that we see
@Override
Public View Makeview () {
TODO auto-generated Method Stub
TextView t = new TextView (mcontext);
T.setgravity (Gravity.center);
T.settextsize (Mheight);
T.setmaxlines (2);
return t;
}
Define action, scroll down page
public void Previous () {
if (Getinanimation ()!= Mindown) {
Setinanimation (Mindown);
}
if (Getoutanimation ()!= Moutdown) {
Setoutanimation (Moutdown);
}
}
Define action, scroll up page
public void Next () {
if (Getinanimation ()!= minup) {
Setinanimation (Minup);
}
if (Getoutanimation ()!= moutup) {
Setoutanimation (Moutup);
}
}

Class Rotate3danimation extends Animation {
Private final float mfromdegrees;
Private final float mtodegrees;
private float Mcenterx;
private float mcentery;
Private Final Boolean mturnin;
Private Final Boolean mturnup;
Private Camera Mcamera;

Public rotate3danimation (float fromdegrees, float todegrees, Boolean turnin, Boolean turnup) {
Mfromdegrees = fromdegrees;
Mtodegrees = todegrees;
Mturnin = Turnin;
Mturnup = Turnup;
}

      @Override
      public void Initialize (int width, int Height, int parentwidth, int parentheight) {
          Super.initialize (width, height, parentwidth, parentheight);
          Mcamera = new Camera ();
          mcentery = GetHeight ()/2;
          Mcenterx = getwidth ()/2;
     }

      @Override
      protected void Applytransformation (float interpolatedtime, transformation t) {
           final float fromdegrees = mfromdegrees;
          Float degrees = fromdegrees + (mtodegrees-fromdegrees) * Interpolatedtime);

Final float CenterX = Mcenterx;
Final float centery = mcentery;
Final Camera Camera = Mcamera;
Final int derection = Mturnup? 1:-1;

Final Matrix matrix = T.getmatrix ();

          Camera.save ();
          if (mturnin) {
               camera.translate (0.0f, derection *mcentery * (interpolatedtime-1.0f), 0.0f);
         } else {
               camera.translate (0.0f, derection *mcentery * (interpolatedtime), 0.0f);
         }
          Camera.rotatex (degrees);
          Camera.getmatrix (matrix);
          Camera.restore ();

Matrix.pretranslate (-centerx,-centery);
Matrix.posttranslate (CenterX, centery);
}
}
}


2. Mainactivity.java
Copy Code code as follows:

Package Com.example.animtextview;

Import Android.os.Bundle;
Import android.app.Activity;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;

public class Mainactivity extends activity implements Onclicklistener {

Private Button Mbtnnext;
Private Button Mbtnprev;
Private Autotextview mTextView02;
private static int scount = 10;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Init ();
}
private void init () {
TODO auto-generated Method Stub
Mbtnnext = (Button) Findviewbyid (R.id.next);
Mbtnprev = (Button) Findviewbyid (R.id.prev);
mTextView02 = (Autotextview) Findviewbyid (R.ID.SWITCHER02);
Mtextview02.settext ("Hello world!");
Mbtnprev.setonclicklistener (this);
Mbtnnext.setonclicklistener (this);
}

@Override
public void OnClick (View arg0) {
TODO auto-generated Method Stub
Switch (Arg0.getid ()) {
Case R.id.next:
Mtextview02.next ();
scount++;
Break
Case R.id.prev:
Mtextview02.previous ();
scount--;
Break
}
Mtextview02.settext (scount%2==0?)
scount+ "Aafirstaa":
scount+ "BBBBBBB");
System.out.println ("Geth: [" +mtextview02.getheight () + "]");

}
}

3. Activity_main.xml

Copy Code code as follows:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:auto3d= "Http://schemas.android.com/apk/res/com.example.animtextview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >

<relativelayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content" >

<button
Android:id= "@+id/next"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_alignparentleft= "true"
Android:layout_alignparenttop= "true"
android:text= "@string/next"/>

        <button
             android:id= "@+id/prev"
            Android:layout_width= "Wrap_content"
            android:layout_height= "Wrap_content"
            Android:layout_alignparentright= "true"
            Android:layout_alignparenttop= "true"
            android:text= "@string/prev"/>
    </relativelayout>

<com.example.animtextview.autotextview
Android:id= "@+id/switcher02"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:background= "@android: Color/holo_green_dark"
Auto3d:textsize= "30sp"/>

</LinearLayout>

The code does not write too many comments, but the structure is still clear, should not ugly understand!

Three summary
I think the difficulty of the implementation of this control is the writing of the animation file, that is, the Rotate3danimation applytransformation (...) The implementation of the method, by controlling the Camara in the Y direction and rotation in the x direction, resulting in a visual sense of tumbling up and down, and then converting the value to the matrix, thus changing the parameters (..., transformation T). Interested friends can directly rewrite the method, you can lose the textswitcher of different animation effects.

Related Article

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.