我如何编码,以便在Android应用程序上裁剪图片?

时间:2016-06-21 04:38:00

标签: java android onclick crop

这是我的java:

public class MainPage extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {

TabLayout tabLayout;
ViewPageAdapter viewPageAdapter;
ViewPager viewPager;
ImageView pfp;
Bitmap bitmap_one;

private String UPLOAD_URL = "http://.php";
private int PICK_IMAGE_REQUEST = 1;
private String KEY_IMAGE = "image";

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

    String username = getIntent().getStringExtra("Username");
    TextView tv = (TextView)findViewById(R.id.usernameND);
    tv.setText(username);

    //id's
    pfp = (ImageView) findViewById(R.id.imageView_one);
    pfp.setOnClickListener(this);


    //SearchIntent
    Intent searchI = getIntent();
    if (Intent.ACTION_SEARCH.equals(searchI.getAction())) {

        String query = searchI.getStringExtra(SearchManager.QUERY);
        Toast.makeText(MainPage.this, query, Toast.LENGTH_SHORT).show();
    }

    //Toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);

    //Tabs
    tabLayout = (TabLayout) findViewById(R.id.tablayout_two);
    viewPager = (ViewPager) findViewById(R.id.viewPager_two);
    viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager());
    viewPageAdapter.addFragments(new FeedFragment(), "Feed");
    viewPageAdapter.addFragments(new MessagesFragment(), "Messages");
    viewPageAdapter.addFragments(new NotificationsFragment(), "Notifications");
    viewPager.setAdapter(viewPageAdapter);
    tabLayout.setupWithViewPager(viewPager);


    //FloatingActionButton
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
        }
    });


    //NavigationDrawer
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.profile) {

        Intent i = new Intent(this, Profile.class);
        startActivity(i);

    } else if (id == R.id.whatshot) {

        Intent i = new Intent(this, WhatsHot.class);
        startActivity(i);

    } else if (id == R.id.trending) {

        Intent i = new Intent(this, Trending.class);
        startActivity(i);

    } else if (id == R.id.radioplayer) {

        Intent i = new Intent(this, Radio.class);
        startActivity(i);

    } else if (id == R.id.musicplayer) {

        Intent i = new Intent(this, MusicPlayer.class);
        startActivity(i);

    } else if (id == R.id.settings) {

        Intent i = new Intent(this, Settings.class);
        startActivity(i);

    } else if (id == R.id.info) {

        Intent i = new Intent(this, Info.class);
        startActivity(i);

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main_menu, menu);

    SearchView searchView = (SearchView) menu.findItem(R.id.search_view).getActionView();
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return super.onCreateOptionsMenu(menu);
}


//ImageInfo
private void showFileChooser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
        Uri filePath = data.getData();
        try {

            bitmap_one = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            pfp.setImageBitmap(bitmap_one);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



//Typeface
private void initTypeface() {

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Amble-Regular.ttf");
    TextView text = (TextView) findViewById(R.id.toolbarTitle);
    text.setTypeface(myTypeface);

    myTypeface = Typeface.createFromAsset(getAssets(), "fonts/Amble-Regular.ttf");
    text = (TextView) findViewById(R.id.usernameND);
    text.setTypeface(myTypeface);

}

@Override
public void onClick(View v) {
    if (v == pfp) {
        showFileChooser();
    }
}

}

这是我的xml:

 <android.support.v4.widget.DrawerLayout      xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
    layout="@layout/app_bar_main_page"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#e9eaea"
    app:itemIconTint="#2A363B"
    app:itemTextColor="#2A363B"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main_page"
    app:menu="@menu/main_page_drawer">

    <ImageView
        android:id="@+id/imageView_one"
        android:layout_width="90dp"
        android:scaleType="fitCenter"
        android:padding="5dp"
        android:background="#2A363B"
        android:adjustViewBounds="true"
        android:onClick="pfpClick"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="20dp"
        android:layout_height="90dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="108dp"
        android:text=""
        android:textColor="#FFf"
        android:textSize="18sp"
        android:id="@+id/usernameND"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_marginTop="126dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />


   </android.support.design.widget.NavigationView>

  </android.support.v4.widget.DrawerLayout>

我想知道的是,如何编写我的应用程序以便在我的imageview(imageView_one)上单击它打开一个窗口或编辑器,这样我就可以将一张图片裁剪成一个正方形,以便它适合并放在Imageview上。这就是我想要弄清楚的。我是android新手所以谢谢你。

1 个答案:

答案 0 :(得分:0)

您可以使用此方法简单地裁剪位图图像。

Bitmap.createScaledBitmap(bitmap, width, height, false);