처음에 안 드 로 이 드 는 방금 만 든 그림 크기 조정 프로그램 을 기록 했다.

8587 단어 그림 크기 조정
안 드 로 이 드 를 시작 하 자마자 허름 한 그림 크기 조정 프로그램 을 썼 다.시간 이 좀 걸 렸 으 니 기록 할 필요 가 있 을 것 같다.
레이아웃 코드:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:layout_gravity="clip_vertical" >

    <LinearLayout

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:gravity="bottom|center_horizontal"

        android:orientation="horizontal" >

        <Button

            android:id="@+id/bsx"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="  " />

        <Button

            android:id="@+id/bfd"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="  " />

    </LinearLayout>

    <LinearLayout

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="vertical" >

        <ImageView

            android:id="@+id/img"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content" >

        </ImageView>

    </LinearLayout>

</FrameLayout>


자바 코드:
package com.example.myscalepic;



import android.os.Bundle;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;

import android.util.DisplayMetrics;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Toast;



public class MainActivity extends Activity implements OnClickListener {

    ImageView imgView;

    Bitmap bmp;

    int screenWidth; //    

    int screenHeight;//    

    float scaleWidth = 1f; //    

    float scaleHeight = 1f;
@Override
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bmp = BitmapFactory.decodeResource(getResources(), R.drawable.feng); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.heightPixels - 80; imgView = (ImageView) this.findViewById(R.id.img); Button b1 = (Button) this.findViewById(R.id.bsx); Button b2 = (Button) this.findViewById(R.id.bfd); // imgView.setImageBitmap(bmp); b1.setOnClickListener(this); b2.setOnClickListener(this); }
@Override
public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View v) { if (v.getId() == R.id.bfd) {// scaleWidth *= 1.2; scaleHeight *= 1.2; } else { // scaleWidth *= 0.8; scaleHeight *= 0.8; } // imgView.setImageBitmap(scaleToFit()); } /** * * @return */ private Bitmap scaleToFit() { int width = bmp.getWidth(); int height = bmp.getHeight(); Matrix ma = new Matrix(); ma.postScale(scaleWidth, scaleHeight); Bitmap tewmpmap; if (width * scaleWidth > screenWidth|| height * scaleHeight > screenHeight) { scaleWidth=1f; scaleHeight=1f; return bmp; } else { // tewmpmap = Bitmap.createBitmap(bmp, 0, 0, width, height, ma, true); return tewmpmap; } } }

좋은 웹페이지 즐겨찾기