2013-08-26 75 views
0

在这段代码中,我想在点击它时更改图片。我怎么能做到这一点?如何更改图片大小

我用img.setOnClickListenser但我真的不知道是什么代码必须放在它

 @Override 
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.gallery_img2, container, false); 
      Log.d("start_new_frg gallery_img ", "ok"); 
      back = (Button)rootView.findViewById(R.id.button1); 

      if (id == -1){ 

       Toast.makeText(getActivity(), "bad Entry ", Toast.LENGTH_LONG).show(); 
       onBackPressed(); 
       getActivity().finish(); 

      } 


      imgv = (ImageView) rootView.findViewById(R.id.imageView1); 

      String ROOT = Environment.getExternalStorageDirectory().getPath()+"/POSTSIMAGES/"; 
      final Bitmap image = BitmapFactory.decodeFile(ROOT+String.valueOf(g.getId())+"q.jpg"); 


      File f = new File(ROOT+String.valueOf(g.getId())+"q.jpg"); 
      temp=ROOT; 
      if(!f.exists()){ 
       imgv.setImageResource(R.drawable.logo_and); 
       } 
      else 
      { 
       imgv.setImageBitmap(image); 

      } 
      if(isNetworkConnected()){ 

       new conn().execute(""); 

      }else{ 
      // pd.dismiss(); 

    //  if(ROOT.endsWith("_s")) 
    //  { 
    //  
    //   
    //  } 
       imgv.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        // your code here 
        Log.d("inside onclick", "ok"); 
        String _n=temp.replace("_s.jpg", "_n.jpg"); 
        imageLoader2.DisplayImage(_n,imgv); 
      // imgv.setScaleType(ImageView.ScaleType.FIT_XY); 

       } 
      }); 

      } 

      //////////////////////// ADS /////////////////////// 

     // Create the adView 
      adView = new AdView(getActivity(), AdSize.SMART_BANNER, "a150f45ea765784"); 

      // Lookup your LinearLayout assuming it's been given 
      // the attribute android:id="@+id/mainLayout" 
      LinearLayout layout = (LinearLayout)rootView.findViewById(R.id.ADS); 

      // Add the adView to it 
      layout.addView(adView); 

      // Initiate a generic request to load it with an ad 
      adView.loadAd(new AdRequest()); 
    //////////////////////// END ADS /////////////////////// 

      back.setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 
        Fragment newFragment = new Gallery(getActivity()); 
        FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

        // Replace whatever is in the fragment_container view with this fragment, 
        // and add the transaction to the back stack 
        transaction.replace(android.R.id.content, newFragment); 
       // transaction.addToBackStack(null); 

        // Commit the transaction 
        // transaction.remove(mFragment); 
        transaction.commit(); 


       } 
      }); 

      return rootView; 

     } 

回答

0

试试这个

final Bitmap image = BitmapFactory.decodeFile(ROOT+String.valueOf(g.getId())+"q.jpg"); 
Bitmap bitmapimage = Bitmap.createScaledBitmap(image, 60, 60, true); 

,或者你可以调用图像的这种方法旁路通道和宽度和高度的大小像60和60

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth, 
      int reqHeight) { 

     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(path, options); 

     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     Bitmap bmp = BitmapFactory.decodeFile(path, options); 
     return bmp; 
     } 

    public static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 

     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 1; 

     if (height > reqHeight || width > reqWidth) { 
      if (width > height) { 
       inSampleSize = Math.round((float) height/(float) reqHeight); 
      } else { 
       inSampleSize = Math.round((float) width/(float) reqWidth); 
      } 
     } 
     return inSampleSize; 
     } 
+0

我已经把这段代码,或者我必须做一些额外的事情,使工作?,,香港专业教育学院只是把它和它不工作,直到如今 –

+0

只是把这个代码,你想要哪一个图像调整大小 –

+0

我放在2个地方,仍然没有工作,首先我把位图声明和第二在其他部分,并在每种情况下它不起作用 –

0

使用相同的代码来创建另一个位图o f想要的图像并将其分配给Imageview。

imgv.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      // your code here 
     final Bitmap new_image = BitmapFactory.decodeFile(ROOT+String.valueOf(g.getId())+"q.jpg"); 
     imgv.setImageBitmap(new_image); 
     } 
     }); 
+0

我试图做到这一点没有看到onclick方法,日志声明不会出现在日志猫 –

+0

你得到它的工作? –