0
我是一個新的Android開發人員,我正在努力與Pinch縮放在ImageView上。此代碼來自以前的應用程序,我修改了我的需要。我的Java知識真的很差。Android需要幫助捏圖像視圖上的縮放
package com.test.testappfour;
import java.io.IOException;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.test.testappfour.TouchImageView;
import com.test.testappfour.R;
public class FloodFillActivity extends SherlockActivity{
\t public static final String IMG = null;
\t Context con;
\t public Bitmap currentbmp;
\t private String imgfile;
\t public int replacecolor;
\t public ImageView showcolor;
\t private AdView mAdView;
\t // \t ImageButton btnsave,btnshare,btnreset;
\t public FloodFillActivity()
\t {
\t \t replacecolor = 0xffff0000;
\t \t imgfile = null;
\t \t currentbmp = null;
\t }
\t @Override
\t protected void onCreate(Bundle savedInstanceState) {
\t \t // TODO Auto-generated method stub
\t \t super.onCreate(savedInstanceState);
\t \t setContentView(R.layout.floodfill);
\t \t getSupportActionBar().setDisplayHomeAsUpEnabled(true);
\t \t getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ffcf28")));
\t \t getSupportActionBar().setDisplayShowHomeEnabled(false);
\t \t getSupportActionBar().setDisplayHomeAsUpEnabled(false);
\t \t con = this;
\t \t mAdView = (AdView) findViewById(R.id.adViewad);
\t \t mAdView.loadAd(new AdRequest.Builder()
\t \t \t \t .build());
\t \t
\t \t imgfile = getIntent().getStringExtra(IMG);
\t \t // \t \t btnsave=(ImageButton)findViewById(R.id.ibtnSave);
\t \t // \t \t btnshare=(ImageButton)findViewById(R.id.ibtnShare);
\t \t // \t \t btnreset=(ImageButton)findViewById(R.id.ibtnReset);
\t \t ImageView imageview = (TouchImageView)findViewById(R.id.floodfill);
\t \t ImageView imageview1 = (TouchImageView)findViewById(R.id.colorpal);
\t \t showcolor = (ImageView)findViewById(R.id.showcolor);
\t \t showcolor.setBackgroundColor(replacecolor);
\t \t try
\t \t {
\t \t \t imageview.setImageDrawable(Drawable.createFromStream(getAssets().open(imgfile), null));
\t \t \t imageview1.setImageDrawable(Drawable.createFromStream(getAssets().open("CATIMAGE/color.png"), null));
\t \t \t imageview1.setOnTouchListener(new android.view.View.OnTouchListener() {
\t \t \t \t Bitmap pmap;
\t \t \t \t public boolean onTouch(View view, MotionEvent motionevent)
\t \t \t \t {
\t \t \t \t \t try
\t \t \t \t \t {
\t \t \t \t \t \t Point point = new Point();
\t \t \t \t \t \t point.x = (int)motionevent.getX();
\t \t \t \t \t \t point.y = (int)motionevent.getY();
\t \t \t \t \t \t ImageView imageview2 = (ImageView)findViewById(R.id.colorpal);
\t \t \t \t \t \t imageview2.buildDrawingCache();
\t \t \t \t \t \t pmap = imageview2.getDrawingCache();
\t \t \t \t \t \t replacecolor = pmap.getPixel(point.x, point.y);
\t \t \t \t \t \t showcolor.setBackgroundColor(replacecolor);
\t \t \t \t \t }
\t \t \t \t \t catch (Exception exception) { }
\t \t \t \t \t return true;
\t \t \t \t }
\t \t \t });
\t \t } catch (Exception exception) { }
\t \t imageview.setOnTouchListener(new OnTouchListener() {
\t \t \t Bitmap bmap;
\t \t \t @Override
\t \t \t public boolean onTouch(View v, MotionEvent event) {
\t \t \t \t // TODO Auto-generated method stub
\t \t \t \t if(event.getAction()==MotionEvent.ACTION_DOWN)
\t \t \t \t {
\t \t \t \t \t Point point = new Point();
\t \t \t \t \t point.x = (int)event.getX();
\t \t \t \t \t point.y = (int)event.getY();
\t \t \t \t \t ImageView imageview2 = (ImageView)findViewById(R.id.floodfill);
\t \t \t \t \t if (bmap == null)
\t \t \t \t \t {
\t \t \t \t \t \t imageview2.buildDrawingCache();
\t \t \t \t \t \t bmap = imageview2.getDrawingCache();
\t \t \t \t \t }
\t \t \t \t \t int i = bmap.getPixel(point.x, point.y);
\t \t \t \t \t int j = replacecolor;
\t \t \t \t \t (new TheTask(bmap, point, i, j, imageview2)).execute(new Void[0]);
\t \t \t \t }
\t \t \t \t return true;
\t \t \t }
\t \t });
\t }
\t
\t
\t @Override
\t public boolean onCreateOptionsMenu(Menu menu) {
\t \t MenuInflater inflater = getSupportMenuInflater();
\t \t inflater.inflate(R.menu.main, menu);
\t \t return super.onCreateOptionsMenu(menu);
\t }
\t @Override
\t public boolean onOptionsItemSelected(MenuItem menuItem)
\t {
\t \t ImageView imageview2 = (ImageView)findViewById(R.id.floodfill);
\t \t final String s ="Image_"+String.valueOf(System.currentTimeMillis());
\t \t switch (menuItem.getItemId())
\t \t {
\t \t case android.R.id.home:
\t \t \t onBackPressed();
\t \t \t return true;
\t \t case R.id.save:
\t \t \t if (currentbmp == null)
\t \t \t {
\t \t \t \t imageview2.buildDrawingCache();
\t \t \t \t currentbmp = imageview2.getDrawingCache();
\t \t \t }
\t \t \t Media.insertImage(getContentResolver(), currentbmp, s, null);
\t \t \t Toast.makeText(getApplicationContext(), "Save to Gallery", Toast.LENGTH_SHORT).show();
\t \t \t return true;
\t \t case R.id.share:
\t \t \t if (currentbmp == null)
\t \t \t {
\t \t \t \t imageview2.buildDrawingCache();
\t \t \t \t currentbmp = imageview2.getDrawingCache();
\t \t \t }
\t \t \t String share =Media.insertImage(getContentResolver(), currentbmp, s, null);
\t \t \t Intent intent = new Intent("android.intent.action.SEND");
\t \t \t intent.putExtra("android.intent.extra.SUBJECT", "Test!");
\t \t \t intent.putExtra("android.intent.extra.STREAM", Uri.parse(share));
\t \t \t intent.setType("image/jpeg");
\t \t \t startActivity(Intent.createChooser(intent, "Testttttt"));
\t \t \t return true;
\t \t case R.id.repeat:
\t \t \t try
\t \t \t {
\t \t \t \t Drawable drawable = Drawable.createFromStream(getAssets().open(imgfile), null);
\t \t \t \t imageview2.setImageDrawable(drawable);
\t \t \t \t imageview2.buildDrawingCache();
\t \t \t }
\t \t \t catch (IOException ioexception1)
\t \t \t {
\t \t \t \t ioexception1.printStackTrace();
\t \t \t }
\t \t \t return true;
\t \t \t
\t \t case R.id.rateapp:
\t \t \t
\t \t \t final String appName = getPackageName();//your application package name i.e play store application url
\t \t \t try {
\t \t \t \t startActivity(new Intent(Intent.ACTION_VIEW,
\t \t \t \t \t \t Uri.parse("market://details?id="
\t \t \t \t \t \t \t \t + appName)));
\t \t \t } catch (android.content.ActivityNotFoundException anfe) {
\t \t \t \t startActivity(new Intent(
\t \t \t \t \t \t Intent.ACTION_VIEW,
\t \t \t \t \t \t Uri.parse("http://play.google.com/store/apps/details?id="
\t \t \t \t \t \t \t \t + appName)));
\t \t \t }
\t \t \t return true;
\t \t \t
\t \t default:
\t \t \t return super.onOptionsItemSelected(menuItem);
\t \t }
\t }
}
這是黑白圖像(圖畫書APP)填充顏色活動,和我用MikeOrtiz TouchImageView實現捏放大。我只是改變了這一行70-71:
\t \t ImageView imageview = (TouchImageView)findViewById(R.id.floodfill);
\t \t ImageView imageview1 = (TouchImageView)findViewById(R.id.colorpal);
to
\t \t TouchImageView imageview = (TouchImageView)findViewById(R.id.floodfill);
\t \t TouchImageView imageview1 = (TouchImageView)findViewById(R.id.colorpal);
我得到這個錯誤: 產生的原因:java.lang.ClassCastException:android.widget.ImageView不能轉換爲com.test.testappfour.TouchImageView
你能幫我一下嗎?謝謝!