android image scaling

2010-12-01  来源:本站原创  分类:Mobile  人气:185 

Sent a one in front of how to use Matrix to rotate the image, then how to scale the image it? In fact, rotation and scaling, as is the need to use different methods to set the Matrix and postScale multiple image scaling. The following example can be up and down keys to zoom and enlarge images. As for how to rotate the image we see here it http://byandby.javaeye.com/blog/830668 like these two examples, and just using different methods. I posted the code to come, of course, we need to look at operating results.

android image scaling

android image scaling

android image scaling

Activity01 class

package com.yarin.android.Examples_05_09;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;

public class Activity01 extends Activity {
        private GameView mGameView = null;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);

                mGameView = new GameView(this);

                setContentView(mGameView);
        }

        public boolean onKeyUp(int keyCode, KeyEvent event) {
                super.onKeyUp(keyCode, event);
                return true;
        }

        public boolean onKeyDown(int keyCode, KeyEvent event) {
                if (mGameView == null) {
                        return false;
                }
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                        this.finish();
                        return true;
                }
                return mGameView.onKeyDown(keyCode, event);
        }
}

GameView

package com.yarin.android.Examples_05_09;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class GameView extends View implements Runnable
{
        /*  Declare a Bitmap object   */
        Bitmap  mBitQQ  = null;
        int             BitQQwidth      = 0;
        int             BitQQheight     = 0;

        float   Scale   = 1.0f;

        /*  Construction of Matrix objects   */
        Matrix mMatrix = new Matrix();

        public GameView(Context context)
        {
                super(context);

                /*  Mount resources   */
                mBitQQ = ((BitmapDrawable) getResources().getDrawable(R.drawable.xh)).getBitmap();

                /*  Get the width and height of the picture   */
                BitQQwidth = mBitQQ.getWidth();
                BitQQheight = mBitQQ.getHeight();

                /*  Open thread   */
                new Thread(this).start();
        }

        public void onDraw(Canvas canvas)
        {
                super.onDraw(canvas);

                /*  Reset  mMatrix */
                mMatrix.reset();

                /*  Set the zoom   */
                mMatrix.postScale(Scale,Scale);

                /*  Press the rotate mMatrix have to build new  Bitmap */
                Bitmap mBitQQ2 = Bitmap.createBitmap(mBitQQ, 0, 0, BitQQwidth,BitQQheight, mMatrix, true);

                /*  Draw pictures rotating after   */
                GameView.drawImage(canvas, mBitQQ2, (320-BitQQwidth)/2, 10);

                mBitQQ2 = null;
        }

        //  Pen events
        public boolean onTouchEvent(MotionEvent event)
        {
                return true;
        }

        //  Key press event
        public boolean onKeyDown(int keyCode, KeyEvent event)
        {
                // On the key
                if (keyCode == KeyEvent.KEYCODE_DPAD_UP)
                {
                        if ( Scale > 0.3 )
                        {
                                Scale-=0.1;
                        }
                }
                // Down arrow key
                else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)
                {
                        if ( Scale < 1.5 )
                        {
                                Scale+=0.1;
                        }
                }
                return true;
        }

        //  Press the key up event
        public boolean onKeyUp(int keyCode, KeyEvent event)
        {
                return false;
        }

        public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
        {
                return true;
        }

        /**
         *  Threading
         */
        public void run()
        {
                while (!Thread.currentThread().isInterrupted())
                {
                        try
                        {
                                Thread.sleep(100);
                        }
                        catch (InterruptedException e)
                        {
                                Thread.currentThread().interrupt();
                        }
                        // Use postInvalidate can directly update the interface in the thread
                        postInvalidate();
                }
        }

        /**
         *  Draw a  Bitmap
         * @param canvas         Canvas
         * @param bitmap         Picture
         * @param x                      The x-coordinate of the screen
         * @param y                      The y-coordinate of the screen
         */
        public static void drawImage(Canvas canvas, Bitmap bitmap, int x, int y)
        {
                /*  Draws the image   */
                canvas.drawBitmap(bitmap, x, y, null);
        }
}

Source code available for download Annex

相关文章
  • android image scaling 2010-12-01

    Sent a one in front of how to use Matrix to rotate the image, then how to scale the image it? In fact, rotation and scaling, as is the need to use different methods to set the Matrix and postScale multiple image scaling. The following example can be

  • Android image stretching and scaling 2010-08-24

    private void initialize () ( DisplayMetrics dm = getResources (). GetDisplayMetrics (); mScreenWidth = dm.widthPixels; / / screen width mScreenHeight = dm.heightPixels; / / screen, high / / MScreenWidth = 100; / / MScreenHeight = 100; / / Zoom / / MS

  • Android Bitmap and Canvas 2010-03-29

    The bitmap is the most commonly used in developing the resources, after all, a pretty interface to users is the most attractive. 1. To get a bitmap from the resource can be used to obtain BitmapDrawable or BitmapFactory bitmap resources. Of course, y

  • Android Permission Daquan from 1.0 SDK record a new access permission to change the 2010-04-27

    Emck My technical career, cracked version of RC30 brush method (Note: Before upgrading the official RC30 ago) November 30, 2008 Online reads as follows: http://forum.xda-developers.com/showthread.php?t=443797 Note: Brush can not ensure that every per

  • Android achieve Jiugongge 2010-03-31

    This need to add / modify three files: main.xml, meunitem.xml, JAVA source code. main.xml the source code is as follows, in itself is a GirdView, used to load Item: <? Xml version = "1.0" encoding = "utf-8"?> <GridView xmlns:

  • Memo --- Android Permission 2010-04-24

    android.permission.ACCESS_CHECKIN_PROPERTIES allows read and write access "properties" table in the checkin database, change the value you can modify upload (Allows read / write access to the "properties" table in the checkin database,

  • Android Bitmap related 2010-05-06

    Bitmap related 1. Bitmap rather special because it can not create but only by means of BitmapFactory Also, according to the source image can be divided into the following situations: * Png image, such as: R.drawable.tianjin Java code Bitmap bmp = Bit

  • Android HammerIt Game Development Experience 2010-05-12

    This is a quick casual game category, click on the screen to hit the target random out of the picture. There are several main functions: 1. Set portraits Open a camera, the above outline of a pig's mask, the goal put the profile inside the head align

  • Android platform bottom drawing 2 2010-05-15

    Today, we continue to introduce Android platform, drawing class, related to the underlying content, the Android UI development project (1) of the interface design of the Android platform, we introduced the use of resources, and Bitmap-related class a

  • Android Permission bilingual 2010-05-26

    android.permission.ACCESS_CHECKIN_PROPERTIES Allows read / write access to the "properties" table in the checkin database, to change values that get uploaded Allows read and write access "properties" table in the checkin database, chan

  • android 1.6 describes all of the rights 2010-05-27

    Manifest.permission This class defines all of the rights of android 106 ACCESS_CHECKIN_PROPERTIES allows read and write access "properties" table in the checkin database, change the value to modify upload Allows read / write access to the "

  • Android permission class and English instructions 2010-05-29

    public final class Manifest { public static final class permission { /** Allows read/write access to the "properties" table in the checkin database, to change values that get uploaded. */ public static final String ACCESS_CHECKIN_PROPERTIES=&quo

  • Android drawing study summary (2) - Bitmap 2010-06-21

    Through the former a study, drawing on the core of Android have a certain understanding of the back pages, we will detail the various drawing objects Android use, begin with our most popular Bitmap (bitmap). Bitmap is the most commonly used developme

  • Android drawing study summary (4) - Animation (on) 2010-06-21

    With a deep understanding of the Drewable discovered Drawable more powerful features: Display Animation. Android SDK introduces two kinds of Animation: * Tween Animation: By constantly doing the scene where the object image transformation (translatio

  • Android drawing study summary (4) - Animation (China) 2010-06-21

    Android drawing in learning conclusion (4) - Animation (I) described in detail Tween Animation in the definition, use, because of space limitations, many important respects not specified in this article on the one hand to be a complete summary descri

  • android robot: Animations 2010-06-25

    Animations Android supports 2 types of animation. Interpolation animation can be applied to rotate, pan, zoom and transition; frame-by-frame animation used to display a series of pictures. On the creation, use and application of a broad overview of a

  • TextView in the xml file to explain android 2010-07-11

    Package Location: android.widget.TextView XML Attributes Attribute Name Related Method Description android: autoLink setAutoLinkMask (int) Controls whether links such as urls and email addresses are automatically found and converted to clickable link

  • Android drawing of the Matrix (1) 2010-07-13

    Matrix, in Chinese called matrix, higher mathematics, presentations, image processing, mainly for flat scaling, translation and rotation operations. First of all, tell us about the matrix operations. Addition and subtraction do not have to say, too s

  • Android started the eighth chapter of the GridView (Magical Squares) 2010-07-15

    GridView is the first choice to achieve the Magical Squares! This article describes how to use the GridView is to achieve the Magical Squares. GridView use of many, the Internet introduced the method is own up to the realization of a ImageAdapter inh

  • Android Permission Daquan (English explanation) 2010-07-18

    Android Permission Daquan from 1.0 SDK with the new changes in the record access permission. Program execution needs to read the safety-sensitive items must declare the relevant authority in androidmanifest.xml request, Android Development Network ha