English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes a method to implement displaying only a part of an image in Android programming. Shared for everyone's reference, as follows:
Loading and displaying an image in an Android application is a very simple task. How can one display only a small part of an image? One approach is to edit the image using photo editing software, save the part to be displayed as a separate image, and then load and display it in the program. However, this will increase the number of images in the program. It is also very simple to cut the part you want from a complete image using the program.
The program implemented below loads an image, then transforms it to fill the entire screen of the phone, and then displays the middle part of the image in the middle of the screen.100*100 part.
ShowPoritionPictureActivity code:
package com.iwin.zzs; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.WindowManager; public class ShowPoritionPictureActivity extends Activity { /** Called when the activity is first created. */ Bitmap picRes; Bitmap showPic; //Get the width and height of the original image int picWidth; int picHeight; private PoritionView poritonView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Do not display the status bar this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); DisplayMetrics dm = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(dm); // Get the length and width of the screen int screenWidth = dm.widthPixels; //Horizontal resolution int screenHeight = dm.heightPixels; //Vertical resolution picRes = BitmapFactory.decodeResource(this.getResources(), R.drawable.girl); // Get the length and width of the image picWidth = picRes.getWidth(); picHeight = picRes.getHeight(); // Calculate the scaling factor, the new size divided by the original size float scaleWidth = ((float) screenWidth ) / picWidth; float scaleHeight = ((float) screenHeight ) / picHeight; // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 缩放图片动作 matrix.postScale(scaleWidth, scaleHeight); // 新得到的图片是原图片经过变换填充到整个屏幕的图片 Bitmap picNewRes = Bitmap.createBitmap(picRes, 0, 0,picWidth, picHeight, matrix, true); // bitmap = Bitmap.createBitmap(400, 480, Bitmap.Config.ARGB_8888); // canvas=new Canvas(); // canvas.setBitmap(bitmap); showPic = Bitmap.createBitmap(picNewRes, screenWidth/2-50, screenHeight/2-50, 100, 100); poritonView = new PoritionView(this); poritonView.setBitmapShow(showPic, screenWidth/2-50, screenHeight/2-50); setContentView(poritonView); }
新建PoritionView类代码:
package com.iwin.zzs; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.view.View; public class PoritionView extends View { private Bitmap showPic = null; private int startX = 0; private int startY = 0; public PoritionView(Context context) { super(context); // 待办自动-生成构造函数存根 } @Override protected void onDraw(Canvas canvas) { // 待办自动-生成方法存根 super.onDraw(canvas); canvas.drawBitmap(showPic, startX, startY, null); } public void setBitmapShow(Bitmap b, int x, int y) { showPic = b; startX = x; startY = y; } }
In the project res/Add the image gir.png to the drawable, and the effect of running the program is to display only the middle of the picture.100*10part of the pictures with 0.
Readers who are interested in more content related to Android can check the special topics on our website: 'Summary of Android Graphics and Image Processing Techniques', 'Android Development Tutorial for Beginners and Advanced', 'Summary of Android Debugging Techniques and Common Problem Solving Methods', 'Summary of Android Multimedia Operation Techniques (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', 'Summary of Android View View Techniques', 'Summary of Android Layout Layout Techniques', and 'Summary of Android Widget Usage'.
I hope the content described in this article will be helpful to everyone in Android program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. The website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email for reporting. Provide relevant evidence, and once verified, the website will immediately delete the content suspected of infringement.