从水平滚动条android获取当前图像

时间:2013-04-11 10:35:53

标签: java android imageview horizontalscrollview

我正在尝试创建一个简单的图片库,通过它我可以设置壁纸;我使用以下代码从下载文件夹中获取文件并在滚动视图中显示它。

我能够做到这一点,但现在我想获取当前显示的图像,以便我可以将该图像设置为壁纸。

以下是我的活动课程代码:

public class MainActivity extends Activity {

 LinearLayout myGallery;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myGallery = (LinearLayout)findViewById(R.id.mygallery);

        String ExternalStorageDirectoryPath = Environment
          .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
          .getAbsolutePath();

        String targetPath = ExternalStorageDirectoryPath ;

        Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
        File targetDirector = new File(targetPath);

        File[] files = targetDirector.listFiles();
        for (File file : files){
         myGallery.addView(insertPhoto(file.getAbsolutePath()));

        }    
    }

    View insertPhoto(String path){
     Bitmap bm = decodeSampledBitmapFromUri(path, 520, 520);

     LinearLayout layout = new LinearLayout(getApplicationContext());
     layout.setLayoutParams(new LayoutParams(550, 550));//Size of view
     layout.setGravity(Gravity.CENTER);

     ImageView imageView = new ImageView(getApplicationContext());
     imageView.setLayoutParams(new LayoutParams(520, 520));
     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
     imageView.setImageBitmap(bm);

     layout.addView(imageView);
     return layout;
    }

    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
     Bitmap bm = null;

     // First decode with inJustDecodeBounds=true to check dimensions
     final BitmapFactory.Options options = new BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     BitmapFactory.decodeFile(path, options);

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

     // Decode bitmap with inSampleSize set
     options.inJustDecodeBounds = false;
     bm = BitmapFactory.decodeFile(path, options); 

     return bm;  
    }

    public int calculateInSampleSize(

     BitmapFactory.Options options, int reqWidth, int reqHeight) {
     // Raw height and width of image
     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;   
    }

}

请告诉我如何获取当前在滚动视图中显示的图像。

由于 阿曼

1 个答案:

答案 0 :(得分:0)

已经5个月没有回答,但我想我会尽力回答。 我认为你需要一个自定义视图即。用帆布。创建一个画布,然后在画布中创建一个位图。之后,当您从URI的形式获得线性布局的位图时,在您的代码中,您将获得一个方法decodeSampledBitmapFromUri,您只需将位图分配给画布上创建的位图。