Android dynamic background implementation and the edittext control added to surfaceview

Source: Internet
Author: User

First of all, we should first look at the case:

The static graph does not show the effect. If you have used this software (Zha ke), all of them know that her background will change. do you think it is very fashionable? At least it is better than static (personal opinion ). in fact, the implementation is not complicated. If it is too child for game programmers to do it, I would like to explain that we should understand the programming thinking of games as well as how much we do in application, at least it is helpful for us to make applications.

The following describes the implementation method.

Implementation principle:CustomizeSurfaceview control. Keep ondraw on, and make the background move.

If you are not familiar with surfaceview, please search for it online.

Here I will briefly introduce its functions:First, this control is a subclass of view. The advantage is that you can update the UI in the thread (non-UI thread.

Mysurfaceview. 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 {private context mcontext; private surfaceholder; private Boolean flag = false; // specifies the private bitmap bitmap_bg thread ID; // background image private float msurfacewindth, msurfaceheight; // screen width and height private int mbitposx; // position of the image private canvas mcanvas; private thread; // move the background status private Enum state {left, ringht} // default value: left private State state = state. left; private final int bitmap_step = 1; // the movement of the background canvas. public mysurfaceview (context, attributeset attrs) {super (context, attrs); flag = true; this. mcontext = context; setfocusable (true); setfocusableintouchmode (true); surfaceholder = getholder (); surfaceholder. addcallback (this);}/***** to draw. */protected void ondraw () {drawbg (); updatebg ();}/***** update the background. */Public void updatebg () {/** image scroll effect **/switch (state) {case left: mbitposx-= bitmap_step; // shift the canvas left break; Case ringht: mbitposx + = bitmap_step; // shift break to the right of the canvas; default: break;} If (mbitposx <=-msurfacewindth/2) {state = state. ringht;} If (mbitposx> = 0) {state = state. left ;}/ ***** draw background */Public void drawbg () {mcanvas. drawcolor (color. transparent, porterduff. mode. clear); // clear the screen. mcanvas. drawbitmap (bitmap_bg, mbitposx, 0, null); // draw the current screen background} @ overridepublic void run () {While (FLAG) {synchronized (surfaceholder) {mcanvas = surfaceholder. lockcanvas (); ondraw (); surfaceholder. unlockcanvasandpost (mcanvas); try {thread. sleep (100);} catch (interruptedexception e) {e. printstacktrace () ;}}}@ overridepublic void surfacecreated (surfaceholder holder) {msurfacewindth = getwidth (); msurfaceheight = getheight (); int mwindth = (INT) (msurfacewindth * 3/2);/***** scale the image to 3/2 times the screen size. */bitmap_bg = bitmaputil. readbitmapbyid (mcontext, R. drawable. hypers_bg, (INT) mwindth, (INT) msurfaceheight); thread = new thread (this); thread. start () ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {}@ overridepublic void surfacedestroyed (surfaceholder holder) {flag = false ;}}

The appeal code is quite simple, and I have not introduced it much. I believe everyone can understand it.

The following is a reference in Main. xml.

<?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 will not show this. it is a time-changing background image. Next I will show you a small internal project I recently developed for the annual gala. (everything in it is moving .) I added a lot of animation effects. It took me a week to create this page.

How about it? It looks good. in fact, I just want to explain something. we can fully integrate the game into our applications, so that we can get another scene. However, this will also have a negative impact, because they are all painted, so the workload will double. in short, you can develop it as needed.

Next I will upload the APK. You can download and install it. Maybe you can find the inspiration.

Hypersparty.apk


Let's look at another page:


I want to explain the edittext problem behind the password. It is very easy to add it. We can solve it through layout. Next we will look at the layout file.


I don't have the layout file code here, because I think it is better to understand this view, isn't it? I have to read the layout file for a long time.

Let me talk about the strange problem. See the following picture:


This picture is the result of clicking the keyboard to make the keyboard a child (Note: The keyboard can block the input box.). In fact, if you click edittext again, it is still in the original state, but surfaceview is affected when being drawn. The reason is unknown .(Next, let me talk about a strange problem. It's just one by one. I have a heart that wants to throw my cell phone. my testing machine is Huawei s8600 and android2.3. Then, when I used the oppo mobile phone android4.0 for testing, I couldn't cut out the above bug picture with a tool. The mobile assistant showed that it was OK, however, the mobile phone shows a bug, nnd, which was depressing at the time. No matter what it is, it is not the point..)

The solution is simple:We only need to execute this section in our activity and it will be OK (also applicable to custom dialog .)

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE| WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Some people on the Internet say that the configuration can be configured in the configuration file, but I have been indifferent to the configuration. You can study it. In short, the Code solution is feasible.

:

The problem arises again. Why is the password so high?

This problem has not been solved. I hope you can tell me if you have any solutions. (If you try to solve this problem multiple times, I personally feel that the system uses edittext as the object and it will pop up, the distance in the middle is related to the location of edittext in the layout. If it is at the bottom, it is on the keyboard ).

Some articles have said that getwindow (). setsoftinputmode (windowmanager. layoutparams. soft_input_adjust_pan); this sentence can be solved, but it is a pity that I have tried it.

In a word, I looked at the past and said it was just like this. I asked in the group yesterday that some friends proposed to create a transparent dialog and activity. Although this was not the case, but it is feasible. As long as you handle it well, it cannot be seen that they are not a layout.


A simple note: in fact, you do not need to add the edittext control to surfaceview. For example, if you want to play a game, a sexy dialog will pop up. there is almost no need to load controls (especially edittext) in it. Even if there is a need, we can change our mindset to implement it.

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.