Android dynamic background implementation as well as adding edittext controls in Surfaceview

Source: Internet
Author: User

      First and foremost, we'll look at the case:                      &N Bsp     Static graphs do not see the effect, if you have used this software (Zake) of the students know that her background will move. How, is not feel very fashionable, at least is better than static (personal view). It's not complicated to realize, if it makes the game programmer do a little pediatrics, Here I explain a point, in fact, we do the application should also know how much to understand the game programming thinking, at least for our application has a good help .      below I briefly introduce the implementation of the way .        The principle of implementation: Customizing a Surfaceview control. To the OnDraw, making its background move .        for Surfaceview if you do not know the students to trouble you first online search, the introduction of a lot of internet related.         Here is a brief introduction to the function: first, the control is a subclass of view. The benefit is that the UI can be updated online approached (non-UI thread) .mysurfaceview.java[java]  Package com.jj.dynamic;    import Android.content.Context;  import Android.graphics.Bitmap;  import Android.graphics.Canvas;  import Android.graphics.Color;  import Android.graphics.Paint;  import Android.graphics.PorterDuff;  import Android.util.AttributeSet;  import Android.view.SurfaceHolder;  import Android.view.SurfaceView;  import Android.view.SurfaceHolder.Callback;    public class Mysurfaceview extends Surfaceview implements Callback, Runnable {     PRIV Ate Context mcontext;      Private Surfaceholder surfaceholder;      Private Boolean flag = false;//thread ID      Private Bitmap bitmap_bg;//background   & nbsp;    Private float msurfacewindth, msurfaceheight;//screen height        private int MBITP osx;//Picture location        private Canvas Mcanvas;        private thread thread;       //Background movement status      private enum State {         left, Ringht     }       //default to left      private state state = State.lef T        private final int bitmap_step = 1;//background canvas moves pace.        Public Mysurfaceview (context context, AttributeSet attrs) {   &nbsp     Super (context, attrs);          flag = true;          This.mcontext = context;          setfocusable (true);          Setfocusableintouchmode (true);          Surfaceholder = Getholder ();          Surfaceholder.addcallback (this);            /***      * to draw .      */& nbsp;    protected void OnDraw () {         DRAWBG (),          UPDATEBG ();            /***      * update background .      */& nbsp;    public void Updatebg () {        /** picture scrolling effects **/       &N Bsp Switch (state) {         Case left:          &NBsp   MBITPOSX-= bitmap_step;//Canvas left shift              break;          case ringht:              MBITPOSX + = bitmap_step;// Canvas right Shift              break;            default:              break;         }          if (mbitposx <=-msurfacewindth/2) { &nbs P           state = State.ringht;         }          if (mbitposx >= 0) {       & nbsp     state = State.left;         }     }       /***      * Draw background       */     public void Drawbg () {         Mcanvas.draw Color (Color.transpareNT, PorterDuff.Mode.CLEAR);//clear the screen.          Mcanvas.drawbitmap (BITMAP_BG, mbitposx, 0, NULL);//Draw current screen background     } &nbsp ;      @Override      public void Run () {        -while (flag ) {             synchronized (surfaceholder) {         &NBSP ;       Mcanvas = Surfaceholder.lockcanvas ();                  OnDraw ();                  surfaceholder.unlockcanvasandpost (Mcanvas);                  try {                     Thread.Sleep (100);                 } catch (Interruptedexception e) {     &nbsp ;               e.printstacktrace ();                              }  &nbsp ;      }       }        @Override      Pub LIC void surfacecreated (Surfaceholder holder) {           msurfacewindth = GetWidth () ;          msurfaceheight = GetHeight ();          int mwindth = (int) (msurfacewindth * 3/2);         /***          * Zoom the picture to the screen 3/twice times .       & nbsp  */         BITMAP_BG = Bitmaputil.readbitmapbyid (Mcontext, R.DRAWABLE.HYPERS_BG,                   (int) mwindth, (int) msurfaceheight);            thread = new Thread (this);          Thread.Start ();       }        @Override      public void surfacechanged (Surfaceholder holder, I NT format, int width,              int height) {      } &nbs p;      @Override      public void surfacedestroyed (Surfaceholder holder) { & nbsp       flag = false;         }   The appeal code is fairly simple, and I won't be able to introduce it. I believe we all can understand. Here's what we just need to refer to in Main.xml. [Java] <?xml version= "1.0" encoding= "Utf-8"?>  <com.jj.dynamic.mysurfaceview xmlns:android= " Http://schemas.android.com/apk/res/android "     android:layout_width=" Fill_parent "     android:layout_height= "fill_parent" >    </com.jj.dynamic.MySurfaceView>   I'm not going to show you this. is a constantly changing background picture, below I show I recently in the development of an internal small project for the opening of the annual entertainment. (Everything is moving.) Look is simple I added a lot of animation effects, this page took me one weeks .                        What do you think It looks pretty good. In fact, I just want to make a point. We can completely integrate the game into our application, so that we can get another picture, but then again, this will bring the corresponding negative effect, because it is painted, so the workload will be doubled. In short, according to their own needs to develop the good. Below I upload apk, you can download Install it and look, maybe you can find inspiration from it. hypersparty.apk  below we look at a page: I want to explain that the password behind the edittext problem, to add to go very simple, we can directly through the layout, below we look at the layout file here I did not on the layout file code, It's because I think it's better to see this view, isn't it, a lot of the layout file Phala Phala posted out, Still have to read half a day. I'll say the weird question. Look at the picture below: This picture is the result of my keyboard when I clicked on the keyboard (note: The soft keyboard can block the input box at this time). In fact, if you click EditText again, in fact, it is still in place, just surfaceview in the time of drawing influence, The reason is unclear. (Below I say a more bizarre question, really one by one, I have to fall on the phone's heart has been.) My test machine is Huawei S8600, android2.3, and then I use oppo mobile phone android4.0 test when the tool unexpectedly cut out the above this bug picture, mobile phone assistant display is OK, but the phone shows is bug,nnd, then depressed to die, regardless of this is not the point. The solution is simple: we just need to execute this phrase in our activity to be OK. (The same applies to custom dialog.) [Java]  getwindow (). Setsoftinputmode (         WindowManager.LayoutParams.SOFT_INPUT_ Adjust_resize                  | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);   Online Some say can be configured in the configuration file, but I configured unexpectedly indifferent. We can go to research, in short, code this solution feasible .:  problem again out, look how so awkward, why the password ran so high? Hope that the students have a solution to the friends of the trouble to inform, (try not many times, the personal feeling system is to edittext forLike, it bounces, the middle distance and the EditText itself in the layout of the position, if at the bottom, then just above the keyboard). There are articles saying   GetWindow (). Setsoftinputmode (WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); This sentence can be resolved, but tried to try disappointments .  in short look at also said the past, temporary on this, yesterday in the group asked, have friends put forward, get a transparent dialog,activity, think although not so, but really feasible, as long as you handle well, It's not like they're a layout. .  the last simple explanation: In fact, in Surfaceview generally do not need to add edittext control, such as the game, even if you want to, It will also pop up a sexy dialog. in which loading controls (especially EditText) there is almost no such demand, even if there is, we will be a change of thinking to achieve OK.

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.