从一个活动移到一个图像到另一个活动在android系统上selcting的GridView [英] move one image from one activity to another activity in android on selcting gridview

查看:108
本文介绍了从一个活动移到一个图像到另一个活动在android系统上selcting的GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含图像网格视图时,点击一张图片上,应该在一个又一个的page.ie打开我需要从一个活动传递到另一个形象,我想获得image.i的像素具有code,以获得pixels.what我需要的是,我必须表明在花药活动图像,使得到pixels.how到选定的图像从一个活动传递到另一个机器人。我已经尝试了一些从堆栈溢出解决方案,但它并没有工作。

// GridviewActivity.java


  

包gr.ed.vi;


 进口android.app.Activity;
              进口android.content.Context;
                进口android.content.Intent;
           进口android.os.Bundle;
           进口android.view.View;
           进口android.view.ViewGroup;
           进口android.widget.AdapterView;
            进口android.widget.BaseAdapter;
              进口android.widget.GridView;
             进口android.widget.ImageView;
                   进口android.widget.Toast;
            进口android.widget.AdapterView.OnItemClickListener;      公共类GridviewActivity扩展活动
    {
    // ---图像显示---
     整数[] = imageIDs {
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher
};@覆盖
公共无效的onCreate(捆绑savedInstanceState)
{
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);    GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
    gridView.setAdapter(新ImageAdapter(本));    gridView.setOnItemClickListener(新OnItemClickListener()
    {
        公共无效onItemClick(适配器视图父母,
        视图V,INT位置,长的ID)
        {
           / * Toast.makeText(getBaseContext(),
                    PIC+(位置+ 1)+选中,
                    Toast.LENGTH_SHORT).show(); * /
            开关(位置){
           情况下0:           打破;
           情况1:
           //调用第二课堂
              意向意图=新意图(GridviewActivity.this,sm.class);         // intent.putExtra(照片,imageIDs);
           // Log.d(TAGjpegCallback1+数据);
            startActivity(意向);             // intent.putExtra(IMG,imageIDs);
             // startActivity(意向);           打破;
           案例2:
           //调用三等功
               意图K1 =新意图(GridviewActivity.this,sm.class);
               startActivity(K1);
           打破;           默认:
           打破;
               }
        }
    });
   }   公共类ImageAdapter延伸BaseAdapter
    {
    私人上下文的背景下;    公共ImageAdapter(上下文C)
    {
        上下文= C;
    }    // ---返回图像的数量---
    公众诠释的getCount(){
        返回imageIDs.length;
    }    // ---返回一个项目的ID ---
    公共对象的getItem(INT位置){
        返回的位置;
    }    众长getItemId(INT位置){
        返回的位置;
    }    // ---返回一个ImageView的看法---
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        ImageView的ImageView的;
        如果(convertView == NULL){
            ImageView的=新ImageView的(上下文);
            imageView.setLayoutParams(新GridView.LayoutParams(85,85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5,5,5,5);
        }其他{
            ImageView的=(ImageView的)convertView;
        }
        imageView.setImageResource(imageIDs [位置]);
        返回ImageView的;
       }
   }
  }

sm.java

 包gr.ed.vi;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.BlurMaskFilter;
进口android.graphics.Canvas;
进口android.graphics.Color;
进口android.graphics.Paint;
进口android.graphics.PorterDuffXfermode;
进口android.graphics.Rect;
进口android.graphics.AvoidXfermode.Mode;
进口android.graphics.Bitmap.Config;
进口android.graphics.BlurMaskFilter.Blur;
进口android.os.Bundle;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.Window;
进口android.view.WindowManager;公共类SM延伸活动{
/ **当第一次创建活动调用。 * /
位图覆盖;
涂料pTouch;
INT X = -100;
INT Y = -100;
帆布带C2;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    捆绑额外= getIntent()getExtras()。
    字节[] =照片extras.getByteArray(照片);
    位图mBitmap = BitmapFactory.de codeByteArray的(照片,0,photo.length);
    位图mBitmapover = BitmapFactory.de codeByteArray的(照片,0,photo.length);             getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
     WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
   //位图mBitmap = BitmapFactory.de codeResource(getResources()
    R.drawable.android);
   //位图mBitmapover = BitmapFactory.de codeResource(getResources(),R.drawable.ss);
    覆盖=
    BitmapFactory.de codeResource
   (getResources(),R.drawable.ss).copy(Config.ARGB_8888,真);
    C2 =新的Canvas(叠加);    pTouch =新的油漆(Paint.ANTI_ALIAS_FLAG);
  // pTouch.setXfermode(新PorterDuffXfermode(Mode.TARGET);
    pTouch.setColor(Color.TRANSPARENT);
    pTouch.setMaskFilter(新BlurMaskFilter(15,Blur.NORMAL));
    的setContentView(新BitMapView(这一点,mBitmap,mBitmapover));
}类BitMapView扩展视图{
    位图mBitmap = NULL;
    位图mBitmapover = NULL;    公共BitMapView(上下文的背景下,位图BM,位图bmover){
    超级(上下文);
    mBitmap = BM;
    mBitmapover = bmover;
    }
     @覆盖
     公共布尔onTouchEvent(MotionEvent EV){         开关(ev.getAction()){             案例MotionEvent.ACTION_DOWN:{                 X =(int)的ev.getX();
                 Y =(INT)ev.getY();
                 无效();                 打破;
             }             案例MotionEvent.ACTION_MOVE:{                     X =(int)的ev.getX();
                     Y =(INT)ev.getY();
                     无效();
                     打破;             }             案例MotionEvent.ACTION_UP:                 打破;         }
         返回true;
     }
    @覆盖
    保护无效的onDraw(帆布油画){
    //当视图称为绘制
    涂料粉刷=新的油漆();
    paint.setFilterBitmap(真);
    //图像将被调整,因此将填补宽度,
    //高度将preserve的image’的长宽比
/ *双的aspectRatio =((双)mBitmap.getWidth())/ mBitmap.getHeight();
    矩形DEST =新的Rect(0,0,this.getWidth(),(INT)(this.getHeight()/的aspectRatio));
    双aspectRatio2 =((双)mBitmapover.getWidth())/ mBitmapover.getHeight();
    矩形dest2 =新的Rect(0,0,this.getWidth(),(INT)(this.getHeight()/ aspectRatio2));
    canvas.drawBitmap(mBitmap,空,DEST,油漆);
    canvas.drawBitmap(mBitmapover,空,dest2,油漆); * /    //绘制背景
    canvas.drawBitmap(mBitmap,0,0,NULL);
    //复制默认覆盖到临时覆盖,并在其打眼
    c2.drawBitmap(mBitmapover,0,0,NULL); //排除该行以显示所有你画
    c2.drawCircle(X,Y,80,pTouch);
    //绘制叠加在背景上
    canvas.drawBitmap(叠加,0,0,NULL);
    }
}


解决方案

*的当您单击图像获取图像的paht / uri和创造的意图,把价值与意图,并把它在另一个活动,证明这一点。

例如:

gridview.setOnItemClickListener(新OnItemClickListener(){

 公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
              INT I = R.drawable.ic_launcher;             意向意图=新意图(SamaaActivity.this,kl.class);
            intent.putExtra(intVariableName我);
            startActivity(意向);
    }
});

和得到这个在其他活动

 意图mIntent = getIntent();
    INT的intValue = mIntent.getIntExtra(intVariableName,0);

i have a grid view which contain images when click on one image it should open in a another page.ie i need to pass the image from one activity to another where i want to get the pixels of the image.i have the code to get the pixels.what i need is that i have to show the image in anther activity so that to get the pixels.how to pass the selected image from one activity to another in android .i have tried some of the solution from stack overflow but it did not worked

//GridviewActivity.java

package gr.ed.vi;

          import android.app.Activity;
              import android.content.Context;
                import android.content.Intent;
           import android.os.Bundle;
           import android.view.View;
           import android.view.ViewGroup;
           import android.widget.AdapterView;
            import android.widget.BaseAdapter;
              import android.widget.GridView;
             import android.widget.ImageView;
                   import android.widget.Toast;
            import android.widget.AdapterView.OnItemClickListener;

      public class GridviewActivity extends Activity
    {    
    //---the images to display---
     Integer[] imageIDs = {
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher                    
};

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

    GridView gridView = (GridView) findViewById(R.id.gridview);
    gridView.setAdapter(new ImageAdapter(this));

    gridView.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView parent, 
        View v, int position, long id) 
        {                
           /* Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();*/
            switch(position){
           case 0:

           break;
           case 1:
           //call second class
              Intent intent = new Intent(GridviewActivity.this, sm.class);

         //  intent.putExtra("photo",imageIDs);
           //  Log.d(TAG, "jpegCallback1" +data);
            startActivity(intent); 

             // intent.putExtra("img", imageIDs);
             //startActivity(intent);

           break;
           case 2:
           //call third class
               Intent k1=new Intent(GridviewActivity.this,sm.class);
               startActivity(k1);
           break;

           default:
           break;
               }
        }
    });        
   }

   public class ImageAdapter extends BaseAdapter 
    {
    private Context context;

    public ImageAdapter(Context c) 
    {
        context = c;
    }

    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }

    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        ImageView imageView;
        if (convertView == null) {
            imageView = new ImageView(context);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(5, 5, 5, 5);
        } else {
            imageView = (ImageView) convertView;
        }
        imageView.setImageResource(imageIDs[position]);
        return imageView;
       }
   }    
  }

sm.java

package gr.ed.vi;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.AvoidXfermode.Mode;
import android.graphics.Bitmap.Config;
import android.graphics.BlurMaskFilter.Blur;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class sm extends Activity {
/** Called when the activity is first created. */
Bitmap overlay;      
Paint pTouch;
int X = -100;
int Y = -100;
Canvas c2;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle extras = getIntent().getExtras();
    byte[] photo = extras.getByteArray("photo");
    Bitmap mBitmap  = BitmapFactory.decodeByteArray (photo, 0, photo.length);
    Bitmap mBitmapover  = BitmapFactory.decodeByteArray (photo, 0, photo.length);

             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,              
     WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
   // Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),         
    R.drawable.android);
   // Bitmap mBitmapover = BitmapFactory.decodeResource(getResources(), R.drawable.ss);
    overlay =   
    BitmapFactory.decodeResource                 
   (getResources(),R.drawable.ss).copy(Config.ARGB_8888, true);  
    c2 = new Canvas(overlay);

    pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);         
  //  pTouch.setXfermode(new PorterDuffXfermode(Mode.TARGET); 
    pTouch.setColor(Color.TRANSPARENT);
    pTouch.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
    setContentView(new BitMapView(this, mBitmap,mBitmapover));
}

class BitMapView extends View {
    Bitmap mBitmap = null;
    Bitmap mBitmapover = null;

    public BitMapView(Context context, Bitmap bm, Bitmap bmover) {
    super(context);
    mBitmap = bm;
    mBitmapover = bmover;
    }
     @Override
     public boolean onTouchEvent(MotionEvent ev) {

         switch (ev.getAction()) {

             case MotionEvent.ACTION_DOWN: {

                 X = (int) ev.getX();
                 Y = (int) ev.getY();
                 invalidate();

                 break;
             }

             case MotionEvent.ACTION_MOVE: {

                     X = (int) ev.getX();
                     Y = (int) ev.getY();
                     invalidate();
                     break;

             }           

             case MotionEvent.ACTION_UP:

                 break;

         }
         return true;
     }


    @Override
    protected void onDraw(Canvas canvas) {
    // called when view is drawn
    Paint paint = new Paint();
    paint.setFilterBitmap(true);
    // The image will be scaled so it will fill the width, and the
    // height will preserve the image’s aspect ration
/*  double aspectRatio = ((double) mBitmap.getWidth()) / mBitmap.getHeight();
    Rect dest = new Rect(0, 0, this.getWidth(),(int) (this.getHeight() / aspectRatio));
    double aspectRatio2 = ((double) mBitmapover.getWidth()) / mBitmapover.getHeight();
    Rect dest2 = new Rect(0, 0, this.getWidth(),(int) (this.getHeight() / aspectRatio2));
    canvas.drawBitmap(mBitmap, null, dest, paint);
    canvas.drawBitmap(mBitmapover, null, dest2, paint);                    */

    //draw background
    canvas.drawBitmap(mBitmap, 0, 0, null);
    //copy the default overlay into temporary overlay and punch a hole in it                          
    c2.drawBitmap(mBitmapover, 0, 0, null); //exclude this line to show all as you draw
    c2.drawCircle(X, Y, 80, pTouch);
    //draw the overlay over the background  
    canvas.drawBitmap(overlay, 0, 0, null);
    }
}

解决方案

*when you click a image get the paht/uri of that image and create intent and put the value with intent and get it in another activity and show it.

ex:

gridview.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
              int i=R.drawable.ic_launcher;

             Intent intent = new Intent(SamaaActivity.this,kl.class); 
            intent.putExtra("intVariableName", i);
            startActivity(intent);
    }
});

and get this on other activity

Intent mIntent = getIntent();
    int intValue = mIntent.getIntExtra("intVariableName", 0);

这篇关于从一个活动移到一个图像到另一个活动在android系统上selcting的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆