Android practice-DEMO (JAVA keyword learning) 1

Source: Internet
Author: User

Android practice-DEMO (JAVA keyword learning) 1

The best way to learn technology is practice. There are too many things you can't see when reading a book. In practice, you may encounter problems that cannot be found in various books. Solving these problems will quickly improve your ability, you are a solider. The best way to grow is practice. Next we will introduce a small DEMO made by myself: JAVA keyword learning, which can be simply understood as an e-book, but some technologies are also used to help beginners improve their abilities.

The first graph of the program is used to introduce the function:

The second page goes to the topic and introduces the Abstract Keyword:

Summary: This DEMO shares the following technologies:

1. How to solve the problem of multi-text layout;

2. How to achieve better gesture paging;

3. How to Achieve the Intent jump animation effect;

4. How to Implement TTS;

5. How to Implement the animation effect of the program.

Next we will solve these problems one by one. For how to solve the problem of multi-text formatting, I thought of html and webview, and prepared html pages in advance and saved them in the asset folder, use the webview control to display html, so that the text effect is better.

Why do we use better gestures to flip pages? We know that the GestureDetector class is used to flip the pages of gestures. For more information, see the following code,

Take the first page as an example:

Package org. yayun. keywordReader; import java. util. locale; import net. youmi. android. adManager; import net. youmi. android. banner. adSize; import net. youmi. android. banner. adView; import net. youmi. android. spot. spotManager; import android. app. activity; import android. content. context; import android. content. intent; import android. graphics. typeface; import android. OS. bundle; import android. speech. tts. textToSpeech; impo Rt android. util. log; import android. view. display; import android. view. gestureDetector; import android. view. gestureDetector. onGestureListener; import android. view. menu; import android. view. menuItem; import android. view. motionEvent; import android. view. view; import android. view. view. onTouchListener; import android. view. window; import android. view. windowManager; import android. webkit. webView; import android. widg Et. imageButton; import android. widget. linearLayout; import android. widget. textView; import android. widget. toast; public class extends act_activity extends Activity {private TextToSpeech tts; private ImageButton ImageButton01; private WebView webView = null; private extends detector; private TextView textView = null; private TextView yinbiaoTextView = null; private int mWidth; public boolean onCreateOpti OnsMenu (Menu menu ){//!!!!!!!!!!!!?? Menu. add (Menu. NONE, Menu. FIRST + 1, 0, exit ). setIcon (null); menu. add (Menu. NONE, Menu. FIRST + 2, 1, about us ). setIcon (null); menu. add (Menu. NONE, Menu. FIRST + 3, 2, check for updates ). setIcon (null); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item ){//!!!!!!!!!!!!?? Switch (item. getItemId () {case Menu. FIRST + 1: finish (); break; case Menu. FIRST + 2: break; case Menu. FIRST + 3: break; default: break;} return false;} public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // life cycle method SpotManager. getInstance (this ). loadSpotAds (); SpotManager. getInstance (this ). showSpotAds (this); AdManager. getInstance (this ). init (c5fbddafb37a478e, eee1729b466a9d36, fals E); requestWindowFeature (Window. FEATURE_NO_TITLE); // fill in the title ?? Super. setContentView (R. layout. abstract_layout); // sets the layout management to be used ?? AdView adView = new AdView (this, AdSize. FIT_SCREEN); // Add the advertisement LinearLayout adLayout = (LinearLayout) findViewById (R. id. adLayout); // Add ad adLayout. addView (adView); // Add ad tts = new TextToSpeech (this, ttsInitListener); ImageButton01 = (ImageButton) this. findViewById (R. id. imageButton01); detector = new GestureDetector (new GestureListener (); this. textView = (TextView) super. findViewById (R. id. mytext); this. yinb IaoTextView = (TextView) super. findViewById (R. id. yinbiao); Typeface mFace = Typeface. createFromAsset (getAssets (), font/segoeui. ttf); yinbiaoTextView. setTypeface (mFace); yinbiaoTextView. setText ([? Listen B? Str sort kt]); this. textView. setText (Abstract); this. webView = (WebView) super. findViewById (R. id. myview); webView. loadUrl (file: // android_asset/abstract.html); this. webView. setOnTouchListener (new TouchListener (); this. webView. setLongClickable (true); ImageButton01.setOnClickListener (new ImageButton. onClickListener () {public void onClick (View v) {// TODO Auto-generated method stub/* input the required string */tts. speak (Clerk Ct, TextToSpeech. QUEUE_FLUSH, null) ;}}); mWidth = Abstract_activity.getScreenWidth (this) ;}// obtain the screen private TextToSpeech. onInitListener ttsInitListener = new TextToSpeech. onInitListener () {public void onInit (int status) {// TODO Auto-generated method stub/* using the US time zone is currently not supported ?? */Locale loc = new Locale (us ,,);/*?? Check whether the input time zone */if (tts. isLanguageAvailable (loc) = TextToSpeech. LANG_AVAILABLE) {/* set language */tts. setLanguage (loc);} tts. setOnUtteranceCompletedListener (ttsuterancecompletedlistener) ;}}; private TextToSpeech. onUtteranceCompletedListener ttsuterancecompletedlistener = new TextToSpeech. onUtteranceCompletedListener () {public void onUtteranceCompleted (String utteranceId) {// TODO Auto-generated method stub} }; @ Overrideprotected void onDestroy () {tts. shutdown (); super. onDestroy ();} public static int getScreenWidth (Context context) {WindowManager manager = null; try {manager = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE);} catch (Exception e) {// TODO Auto-generated catch blocke. printStackTrace (); Log. d (11, cuowu1);} Display display = manager. getdefadisplay display (); return display. getWidth ();} Private class TouchListener implements OnTouchListener {public boolean onTouch (View v, MotionEvent event) {return detector. onTouchEvent (event) ;}} private int minVelocity = 0; public class GestureListener implements OnGestureListener {public boolean onDown (MotionEvent e) {// TODO Auto-generated method stubreturn false ;} public void onShowPress (MotionEvent e) {// TODO Auto-generated method stub} publ Ic boolean onSingleTapUp (MotionEvent e) {// TODO Auto-generated method stubreturn false;} public boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {return false ;} public void onLongPress (MotionEvent e) {// TODO Auto-generated method stub} public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {if (e1.getX () -e2.getX ()> mWidth/3 & Ma Th. abs (velocityX)> minVelocity) {// switch ActivityLog. d (11, verticalMinDistance); Intent intent = new Intent (Abstract_activity.this, Assert_activity.class); startActivity (intent); overridePendingTransition (R. anim. fade, R. anim. hold); // Toast. makeText (Abstract_activity.this, gesture to the left, // Toast. LENGTH_SHORT ). show ();} else if (e2.getX ()-e1.getX ()> mWidth/3 & Math. abs (velocityX)> minVelocity) {Toast. makeTex T (Abstract_activity.this, first page !, Toast. LENGTH_SHORT). show () ;}return false ;}}}

Let's look at this piece of code:

Public boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {if (e1.getX ()-e2.getX ()> mWidth/3 & Math. abs (velocityX)> minVelocity) {// switch ActivityLog. d (11, verticalMinDistance); Intent intent = new Intent (Abstract_activity.this, Assert_activity.class); startActivity (intent); overridePendingTransition (R. anim. fade, R. anim. hold); // Toast. makeText (Abstract_activity.this, Left gesture, // Toast. LENGTH_SHORT ). show ();} else if (e2.getX ()-e1.getX ()> mWidth/3 & Math. abs (velocityX)> minVelocity) {Toast. makeText (maid, first page !, Toast. LENGTH_SHORT). show ();} return false ;}


This code is the key code for implementing a gesture paging. e1 indicates the starting point event of the gesture, e2 indicates the end point event of the gesture, and e1.getX ()-e2.getX () it can be understood as the number of pixel points crossed by the hand. The general articles fix their size, such as 100, or 200. In this way, if the resolution of a user's mobile phone is very high, the user experience is very poor and prone to misoperations, that is, it is easy to switch pages when turning pages down. To solve this problem, we should first use the following methods:

public static int getScreenWidth(Context context) {WindowManager manager = null;try {manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();Log.d(11, cuowu1 );}Display display = manager.getDefaultDisplay();return display.getWidth();}


Obtain the width pixel of the screen, and then compare e2.getX ()-e1.getX () with mWidth/3 in the onFling () method, that is, 1/3 screen as a reference, in this way, only 1/3 of the screen can be displayed on a mobile phone at any resolution, effectively preventing misoperations on the screen at a low resolution.

For the third point, it should be relatively simple to implement the animated Effect of Intent jump. We can achieve the animated Effect of turning back pages,

The Android platform provides two types of animations. One is Tween animation, which is used to continuously change the images of objects in the scene to produce the animation effect (rotation, translation, contraction, and gradient ).

The second type is Frame Animation, that is, sequential playback of prepared images, similar to gif images.

The first type is generally used:

Tweene Animations.

 

Main categories:

 

Animation

AlphaAnimation gradient transparency

RotateAnimation image rotation

ScaleAnimation gradient Scaling

TranslateAnimation location Movement

Animation set

 

 

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.