通过imageview的drawacble id

时间:2018-06-08 04:37:54

标签: android

我在Mainactivity中创建了一个图像Gridview,有一些图像命名为image1.jpg,image2.jpg ...当有人点击image1.jpg然后在fullimage活动中,image1.jpg自动打开,如果有人点击image2.jpg然后在完整图像活动中,image2.jpg现在在完整图像活动中自动打开,首先有两件事是imageview,第二件是按钮 这是我的主要活动

package com.cinegoes.www.video;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {


GridView Grid;
GRIDAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final Integer[] image = new Integer[]{R.drawable.image1, 
R.drawable.image2, R.drawable.image3,
                                    R.drawable.image4, R.drawable.image5, 
R.drawable.image6,
                                    R.drawable.image7, R.drawable.image8};

    Grid = (GridView)findViewById(R.id.MyGrid);
    adapter = new GRIDAdapter(getApplicationContext(), R.layout.grid_item, 
image);

    Grid.setAdapter(adapter);
    Grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, 
long id) {
            Intent go = new Intent(getApplicationContext(), Details.class);
            go.putExtra("image", image[position]);
            //By position Clicked
            startActivity(go);
        }
    });

}


public class GRIDAdapter extends ArrayAdapter {
    private Integer[] Image;
    private int resource;
    private LayoutInflater inflater;

    public GRIDAdapter(Context context, int resource, Integer[] image) {
        super(context, resource, image);
        Image = image;
        this.resource = resource;
        inflater = 
(LayoutInflater)MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder Holder = null;

        if (convertView == null) {
            Holder = new ViewHolder();
            convertView = inflater.inflate(resource, null);
            Holder.IMAGE = (ImageView) 
convertView.findViewById(R.id.imageID);
            convertView.setTag(Holder);
        } else {
            Holder = (ViewHolder)convertView.getTag();
        }
        Holder.IMAGE.setImageResource(Image[position]);
        Holder.IMAGE.setScaleType(ImageView.ScaleType.CENTER_CROP);

        return convertView;
    }

    class ViewHolder {
        private ImageView IMAGE;
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

这是我的完整图片活动

package com.cinegoes.www.video;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.ImageView;



public class Details extends AppCompatActivity {

@Override
public void onCreate(Bundle s){
    super.onCreate(s);
    setContentView(R.layout.imagedetail);

    Integer IMAGE = getIntent().getIntExtra("image", 1/*Default Value of 
Int*/);
    Button button = (Button)findViewById(R.id.button2);
    ImageView img = (ImageView)findViewById(R.id.images);
    img.setScaleType(ImageView.ScaleType.CENTER_CROP);
    img.setImageResource(IMAGE);

}
}

我有另外三个活动,我想当有人打开image1.jpg然后在fullimageactivity当按钮点击然后活动one.java将被打开,如果有人打开image2.jpg然后在fullimageactivity当按钮点击然后活动two.java将被打开了 请帮帮我

0 个答案:

没有答案