Android Study Notes (27)-Rewrite webview and change the flash Key Control Value

Source: Internet
Author: User

We all know that the Flash games that are generally downloaded (the one that is played based on the keyboard) are generally controlled by the upper and lower keys. Now I want to use other keys to replace the upper and lower left keys. The implementation process is as follows:

1. Because it is a keyboard control, the use of onkeydown and onkeyup is indispensable. To change the controlled key value in the game, you must overwrite the webview (here webview is used to play flash ).

Rewrite webview

public class mWebView extends WebView {public mWebView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {return true;} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {return true;}if(keyCode == KeyEvent.KEYCODE_L){//KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT);return super.onKeyDown(KeyEvent.KEYCODE_DPAD_LEFT, event);}if(keyCode == KeyEvent.KEYCODE_R){//KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT);return super.onKeyDown(KeyEvent.KEYCODE_DPAD_RIGHT, event);}return super.onKeyDown(keyCode, event);}@Overridepublic boolean onKeyUp(int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {return true;} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {return true;}if(keyCode == KeyEvent.KEYCODE_L){//KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_LEFT);return super.onKeyUp(KeyEvent.KEYCODE_DPAD_LEFT, event);}if(keyCode == KeyEvent.KEYCODE_R){//KeyEvent key = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT);return super.onKeyUp(KeyEvent.KEYCODE_DPAD_RIGHT, event);}return super.onKeyUp(keyCode, event);}}

The above uses the "L" key instead of the left button, and the "R" key instead of the right button. You can modify it by yourself. Note that, because some games have different operations on onkeydown and onkeyup, both onkeydown and onkeyup must be replaced.

Note the following constructor:

public mWebView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}

This is very important. If you use webview in an XML file, the above constructor must not make any mistakes. In fact, this method is used as a callback method when the system parses the attributes defined in XML. Let's take a look at the XML file:

    <com.ideal.viewfliperflash.mWebView        android:id="@+id/webview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_centerInParent="true"        android:background="#00000000" />

Or:

    <view        class="com.ideal.viewfliperflash.mWebView"        android:id="@+id/webview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_centerInParent="true"        android:background="#00000000" />

2. check how the custom webview is used:

public mWebView mWebFlash = null;
mWebFlash = (mWebView) findViewById(R.id.webview);

Flash loading:

public void loadFlash() {WebSettings settings = mWebFlash.getSettings();settings.setPluginsEnabled(true);settings.setJavaScriptEnabled(true);settings.setAllowFileAccess(true);settings.setDefaultTextEncodingName("GBK");mWebFlash.setBackgroundColor(0);mWebFlash.loadUrl(fileCurrent);}

The above filecurrent is the path of the flash file. Call loadflash as needed.

3. If custom webview is not used in the XML file, the previous constructor will not go wrong.

To customize a view, the following addresses are concise and useful. Http://www.cnblogs.com/lovewf/archive/2011/11/28/2264473.html

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.