Android位图图像大小调整

Android位图图像大小调整,android,image,bitmap,resize,size,Android,Image,Bitmap,Resize,Size,所以我正在处理一个imageview,我想调整我项目中图片的大小,我知道我可以用位图来做,所以我改变了,y代码,开始使用位图,当我使用位图时,调整其抛出和erorexption,并从应用程序崩溃 public class ClassicMode extends Activity {//מהמשחק עצמו String pic;//תמונה של הדגל Button answer1;//תשובות Button answer2; Button answer3; Button answ

所以我正在处理一个imageview,我想调整我项目中图片的大小,我知道我可以用位图来做,所以我改变了,y代码,开始使用位图,当我使用位图时,调整其抛出和erorexption,并从应用程序崩溃

public class ClassicMode extends Activity {//מהמשחק עצמו

String pic;//תמונה של הדגל
Button answer1;//תשובות
Button answer2;
Button answer3;
Button answer4;
TextView guess;//
TextView numOfGuess;//
DatabaseHandler db = new DatabaseHandler(this);
String fn;
Guesses G;
Bitmap bm;
//int Nguess=3;//מסםר ניחושים
//int Nguesstemp=Nguess;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    guess =(TextView)findViewById(R.id.guesses);
    numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
    //numOfGuess.setText(Integer.toString(Nguess));
    numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
    Flags f = new Flags();
    Random r = new Random();//הדגל שיבחר לשאלה
    int num = r.nextInt(100);//Up
    //Log.d("yes", "yes");
    //Random ra = new Random();//הדגלים שיהיו בתשובות
    //int numA = ra.nextInt(4);//Up

    f = db.getFlag(num);//הצגת הדגל הרנדומלי שיצא
    //  final Flags d = f; // trying to make it final but it dosent help
    fn = f.getName().toString();
    pic = f.getImage().toString();
    pic_view(pic);//מעבר לפונקציה להשמת התמונה של הדגל במשחק

    //מערך ארבע כפתורים כנגד ארבע תשובות
    Button [] b = new Button[4];
    b[0] = (Button)findViewById(R.id.button1);
    b[1] = (Button)findViewById(R.id.button2);
    b[2] = (Button)findViewById(R.id.button3);
    b[3] = (Button)findViewById(R.id.button4);

    List<String>Answers=new ArrayList<String>();//מערך תשובות
    Answers.add(f.getName().toString());//הוספת התשובה הנכונה
    num = r.nextInt(30);
    Answers.add(db.getFlag(num).getName().toString());//הוספת 3 תשובות רנדומליות
    num = r.nextInt(30);
    Answers.add(db.getFlag(num).getName().toString());
    num = r.nextInt(30);
    Answers.add(db.getFlag(num).getName().toString());

    Collections.shuffle(Answers);//ערבוב התשובות

    for(int i=0;i<Answers.size();i++)
    {
        b[i].setText(Answers.get(i));//השמת התשובות מהמהערך למערך הכפתורים
    }
}//end of OnCreat


@SuppressLint("NewApi")
public void resetQuiz()
{
    //finish();
    recreate();
    //numOfGuess.setText(String.valueOf(Nguess));
}

public void check(View v)
{
    Log.d("yes", fn);
    Button b = (Button)v;
    String text = b.getText().toString();
    if(text.equals(fn))
        resetQuiz();
    else
    {
        if(Guesses.numOfGuesses==1)
        {
            G.setNumOfGuesses(3);
            finish();//כאשר מספר הניחושים
            return;
        }
        Guesses.numOfGuesses--;
        //numOfGuess=(TextView)findViewById(R.id.numOfGuesses);
        //numOfGuess.setText(Integer.toString(Nguess));
        numOfGuess.setText(String.valueOf(Guesses.numOfGuesses));
    }

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.second, menu);
    return true;
}

private void pic_view(String pic2) {

    // TODO Auto-generated method stub
    //גישה לדגל לפי שמו וייבוא התמונה
    Log.d("Result from pic function " , pic2);
    ImageView imageView = (ImageView)findViewById(R.id.imageView1);
    String uri ="@drawable/";
    uri += pic2;    
    int imageResource = getResources().getIdentifier(uri, pic2, getPackageName());//הצוות התמונה 
    Bitmap bm = BitmapFactory.decodeResource(getResources(), imageResource);
    Bitmap resized = Bitmap.createScaledBitmap(bm, 120, 120, true);
    Drawable res= getResources().getDrawable(imageResource);//ציור התמונה
    imageView.setImageDrawable(res);

}
}

调整大小和位图以及所有的图像都在picu视图功能下工作,希望你能帮助我

我的大小问题的图片,因此我想调整标志的大小:

好了,伙计,你能做的就是创建自己的自定义图像视图

    public class ScaleImageView extends ImageView {

      public ScaleImageView(Context context) {
        super(context);
      }

      public ScaleImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
      }

      public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
      }

      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
          Drawable drawable = getDrawable();
          if (drawable == null) {
            setMeasuredDimension(0, 0);
          } else {
            int measuredWidth = MeasureSpec.getSize(widthMeasureSpec);
            int measuredHeight = MeasureSpec.getSize(heightMeasureSpec);
            if (measuredHeight == 0 && measuredWidth == 0) { //Height and width set to wrap_content
              setMeasuredDimension(measuredWidth, measuredHeight);
            } else if (measuredHeight == 0) { //Height set to wrap_content
              int width = measuredWidth;
              int height = width *  drawable.getIntrinsicHeight() / drawable.getIntrinsicWidth();
              setMeasuredDimension(width, height);
            } else if (measuredWidth == 0){ //Width set to wrap_content
              int height = measuredHeight;
              int width = height * drawable.getIntrinsicWidth() / drawable.getIntrinsicHeight();
              setMeasuredDimension(width, height);
            } else { //Width and height are explicitly set (either to match_parent or to exact value)
              setMeasuredDimension(measuredWidth, measuredHeight);
            }
          }
        } catch (Exception e) {
          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
      }
}
您可以根据需要在xml文件中使用此imageView

<com.example.MyPackage.ScaleImageView 
        android:layout_height="20dp"
        android:layout_width="20dp"
        android:src="@drawable/ic_launcher"/>

如果这也帮不上忙,请告诉我:

是否要调整位图的大小以使其适合该视图?是的,在我的xml中| | android:scaleType=centerInsided您是否在scaleType中尝试了fitXY?否,您能告诉我如何编写它吗?android:fitx?android:scaleType=fitXY