如何使GridView的某些元素透明以显示下面的图像?

时间:2016-11-18 12:58:09

标签: android gridview bitmap imageview

你好, 我是Android的新手,我正在开发一个简单的图片库。我将照片变成 public class buttonActionStart implements ActionListener { // overwritten ActionListener public void actionPerformed (ActionEvent e){ JFrame window2 = new JFrame("Main Game"); //create empty frame Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); window2.setSize(screenSize.width,screenSize.height); window2.add(new JPanel(){ public void paintComponent(Graphics graph){ super.paintComponent(graph); Board board = new Board(); graph.drawString(board.sqaures[1].getName(),440,560); graph.drawString(board.sqaures[2].getName(),335,560); graph.drawString(board.sqaures[3].getName(),225,560); graph.drawString(board.sqaures[4].getName(),145,560); graph.drawString(board.sqaures[5].getName(),30,560); graph.drawString(board.sqaures[6].getName(),25,460); graph.drawString(board.sqaures[7].getName(),25,360); graph.drawString(board.sqaures[8].getName(),35,260); graph.drawString(board.sqaures[9].getName(),25,160); graph.drawString(board.sqaures[10].getName(),25,60); graph.drawString(board.sqaures[11].getName(),140,60); graph.drawString(board.sqaures[12].getName(),240,60); graph.drawString(board.sqaures[13].getName(),340,60); graph.drawString(board.sqaures[14].getName(),440,60); graph.drawString(board.sqaures[15].getName(),530,60); graph.drawString(board.sqaures[16].getName(),550,160); graph.drawString(board.sqaures[17].getName(),545,260); graph.drawString(board.sqaures[18].getName(),540,360); graph.drawString(board.sqaures[19].getName(),550,460); for (int i = 0; i < 6; i++) graph.drawRect(10 + (i*100),10,100,100); for (int j = 0; j < 4; j++) graph.drawRect(10,110 + (j*100),100,100); for (int k = 0; k < 4; k++) graph.drawRect(510,110 + (k*100),100,100); for (int l = 0; l < 6; l++) graph.drawRect(10 + (l*100),510,100,100); InputStream resource = UI.class.getResourceAsStream("player1.png"); Image player1Image = null; try{ player1Image = ImageIO.read(resource); }catch (IOException e){ e.printStackTrace(); } resource = UI.class.getResourceAsStream("player2.png"); Image player2Image = null; try{ player2Image = ImageIO.read(resource); }catch (IOException e){ e.printStackTrace(); } resource = UI.class.getResourceAsStream("player3.png"); Image player3Image = null; try{ player3Image = ImageIO.read(resource); }catch (IOException e){ e.printStackTrace(); } resource = UI.class.getResourceAsStream("player4.png"); Image player4Image = null; try{ player4Image = ImageIO.read(resource); }catch (IOException e){ e.printStackTrace(); } graph.drawImage(player1Image, 511, 511, null); graph.drawImage(player2Image, 585, 510, null); graph.drawImage(player3Image, 511, 585, null); graph.drawImage(player4Image, 585, 585, null); } }); window2.setVisible(true); } } ,然后将其剪切成9个方形(或相等)元素。之后,我使用我的Bitmap版本将其放入GridView。最后我想做一个效果,这个正方形随机变得透明(从可见到透明的渐变)并在它们下面再渲染另一个图像(所以这个过程可以无限重复)。我想稍后更改照片ImageAdapter,但现在我无法弄清楚如何使OnClick的某些元素透明。我尝试使用GridViewgetChildAt(int index),但我得到的只是getItemAtPosition(int position)。谁能帮我这个?我尝试在网络上使用许多代码示例,但没有一个能够实现。

我的主要活动:  

NullPointerException

我的适配器:

public class MainActivity extends AppCompatActivity {

    private int screenX;
    private int screenY;
    private ArrayList<Bitmap> chunkedImages;
    private GridView gv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        RelativeLayout rl = (RelativeLayout)findViewById(R.id.RelativeLayout1);
        rl.setBackgroundColor(Color.BLACK);
        loadScreenXY();
        ImageView view = new ImageView(this);
        view.setImageResource(R.drawable.pizza);
        this.chunkedImages = this.splitImage(view,9);
        gv = (GridView) findViewById(R.id.GridView);
        gv.setAdapter(new GridImageAdapter(this,this.chunkedImages));
        gv.setNumColumns(3);
    }

    private void loadScreenXY() {
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        this.screenX = size.x;
        this.screenY = size.y;
    }

    private ArrayList<Bitmap> splitImage(ImageView image, int chunkNumbers) {
        int rows,cols;
        int chunkHeight;
        int chunkWidth;
        ArrayList<Bitmap> chunkedImages = new ArrayList<Bitmap>(chunkNumbers);
        BitmapDrawable drawable = (BitmapDrawable) image.getDrawable();
        Bitmap bitmap = drawable.getBitmap();
        rows = cols = (int) Math.sqrt(chunkNumbers);
        chunkHeight = bitmap.getHeight()/rows;
        chunkWidth = bitmap.getWidth()/cols;
        int yCoord = 0;
        for(int x=0; x<rows; x++){
            int xCoord = 0;
            for(int y=0; y<cols; y++){
                chunkedImages.add(Bitmap.createBitmap(bitmap, xCoord, yCoord, chunkWidth, chunkHeight));
                xCoord += chunkWidth;
            }
            yCoord += chunkHeight;
        }
        return chunkedImages;
    }

0 个答案:

没有答案