考虑显示大小裁剪位图

时间:2015-03-29 00:58:04

标签: android android-layout

我有一个大图像(1024x1024)并且想要裁剪它以便在考虑显示尺寸(例如480x800)的情况下正确适合屏幕。但是,如下所示,它不适合。我怎么能适合它?

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/bgview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        Point size = new Point();
        Display display = getWindowManager().getDefaultDisplay();
        display.getSize(size);
        Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.sample);
        Bitmap bmp2 = Bitmap.createBitmap(bmp, 0, 0, size.x, size.y);
        ImageView bg = (ImageView) findViewById(R.id.bgview);

        bg.setImageBitmap(bmp2);
    }

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Current view Background image

2 个答案:

答案 0 :(得分:1)

在你的ImageView标签中添加: android:scaleType="centerCrop"您也可以尝试android:scaleType="fitXY"

答案 1 :(得分:1)

我建议您使用:

他们网站的示例:

Picasso.with(context)
    .load(url)
    .fit()
    .centerCrop()
    .into(imageView);

使用它并设置android:layout_width="match_parent"以填充屏幕宽度