缩放库中的选定图像

时间:2011-10-24 17:12:02

标签: android gallery

我如何缩放图库中的所选图像,我试过这个,但它不能很好地工作。 `

public void  onItemSelected  (AdapterView<?>  parent, View  v, int position, long id) 
{
    Animation grow = AnimationUtils.loadAnimation(DetailTvShow.this, R.anim.grow);

            Button btn = (Button)findViewById(R.id.button1);
            btn.setText("saison "+(position+1));
            View sideView = v.findViewById(position + 1);
            View sideView1 = v.findViewById(position - 1);

           if ((sideView != null)||(sideView1 != null)){
               ((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(80, 115));

               ((ImageView)sideView1).setLayoutParams(new `enter code here`Gallery.LayoutParams(80, 115));

           }

      /*      sideView = parent.findViewById(position + 1);
          if (sideView != null)
               ((ImageView)sideView).setLayoutParams(new Gallery.LayoutParams(80, 115));  */

            v.startAnimation(grow);
            v.setLayoutParams(new Gallery.LayoutParams(105, 140));
        }`

2 个答案:

答案 0 :(得分:5)

我有同样的问题,有一个类似的解决方案,我将在这里介绍堆栈溢出,但它没有正常工作,虽然视图将扩展,他们最终将停止返回其原始大小。

这里修改后的修复程序我自己发现到目前为止似乎工作正常并且不会导致任何问题。

在res / anim中创建grow.xml文件将比例尺更改为适合您的任何内容

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
       android:fromXScale="1.0"
       android:toXScale="1.075"
       android:fromYScale="1.0"
       android:toYScale="1.075"
       android:duration="150"
       android:pivotX="50%"
       android:pivotY="50%"
       android:interpolator="@android:anim/accelerate_decelerate_interpolator"
       android:fillAfter="true"> 
</scale>

复制此课程并将其放入您继承的画廊或您的活动中

    private class SelectListener implements AdapterView.OnItemSelectedListener {

        private Animation grow     = null;
        private View      lastView = null; 

        public SelectListener(Context c) {
             grow = AnimationUtils.loadAnimation(c, R.anim.grow);
        }

        public void  onItemSelected(AdapterView<?>  parent, View  v, int position, long id)         {

            // Shrink the view that was zoomed 
            try { if (null != lastView) lastView.clearAnimation();
            } catch (Exception clear) { }

            // Zoom the new selected view
            try { v.startAnimation(grow); } catch (Exception animate) {}

            // Set the last view so we can clear the animation 
            lastView = v;
        }

        public void onNothingSelected(AdapterView<?>  parent) {
        }

   }

然后,在创建了库类并附加适配器调用

之后
yourGalleryName.setOnItemSelectedListener(new SelectListener(this));  

这对我有用,请告诉我是否也适合你。

答案 1 :(得分:1)

也许这会帮助你走上正轨:

public class bitmaptest extends Activity {
@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    LinearLayout linLayout = new LinearLayout(this);

    // load the origial BitMap (500 x 500 px)
    Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), 
           R.drawable.android);

    int width = bitmapOrg.width();
    int height = bitmapOrg.height();
    int newWidth = 200;
    int newHeight = 200;

    // calculate the scale - in this case = 0.4f
    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate(45);

    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                      width, height, matrix, true); 

    // make a Drawable from Bitmap to allow to set the BitMap 
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    ImageView imageView = new ImageView(this);

    // set the Drawable on the ImageView
    imageView.setImageDrawable(bmd);

    // center the Image
    imageView.setScaleType(ScaleType.CENTER);

    // add ImageView to the Layout
    linLayout.addView(imageView, 
            new LinearLayout.LayoutParams(
                  LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
            )
    );

    // set LinearLayout as ContentView
    setContentView(linLayout);
}

}