ImageView setImageBitmap方法会弄乱整个布局

时间:2012-03-29 08:02:12

标签: android android-layout imageview

有一个布局,我有一些按钮,控件和一个imageView。当布局显示时,一切都很好。但是后来,当下载请求的图像(aveform的图像)时,我尝试使用setImageBitmap将其设置为ImageView。整个布局变得搞砸了。这是一些照片。

当未下载图像时,这就是布局的样子: enter image description here 这是在setImageBitMap之后 enter image description here

这是我的xml(我使用了imageView的自定义子类,但错误也出现在原始的mageView中)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:gravity="center"
>

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

     >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center" >

        <adam.czibere.WaveFormSurfaceView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             />

    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:thumb="@drawable/progress_fill" />

    </TableRow>


    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" >

        <TextView
            android:id="@+id/currentPos"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="right"
            android:layout_weight="5"
            android:text="00:00.000" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_weight="1"
            android:text="/" />

        <TextView
            android:id="@+id/endPos"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:gravity="left"
            android:text="00:00.000" />
    </TableRow>


    <TableRow
        android:id="@+id/tableRow4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/closebtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Close" />

        <Button
            android:id="@+id/playButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Play" />

        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="ToggleButton" />

    </TableRow>

</TableLayout>

这是扩展的imageView:

public class WaveFormSurfaceView extends ImageView {

public boolean isEditMode = false;
Bitmap waveform = null;
int touchCount = 0;
List<VerbaComment> myComments = new ArrayList<VerbaComment>();
int lastTouch_x = -1;

public WaveFormSurfaceView(Context context) {
    super(context);

}

public WaveFormSurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public WaveFormSurfaceView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

// copies the given waveform to a variable
public void setWaveForm(Bitmap b) {

    waveform = Bitmap.createBitmap(b.getWidth(), b.getHeight(),
            b.getConfig());
    // copy the pixel to it
    int[] allpixels = new int[b.getHeight() * b.getWidth()];
    b.getPixels(allpixels, 0, b.getWidth(), 0, 0, b.getWidth(),
            b.getHeight());
    waveform.setPixels(allpixels, 0, b.getWidth(), 0, 0, b.getWidth(),
            b.getHeight());

}

@Override
public void setImageBitmap(Bitmap bm) {
    // TODO Auto-generated method stub
    super.setImageBitmap(bm);
    setWaveForm(bm);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO Auto-generated method stub
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    // System.out.println("waveform width: "+waveform.getWidth());

    // setMeasuredDimension(getMeasuredWidth(), getMeasuredHeight());

    // setMeasuredDimension(widthMeasureSpec,heightMeasureSpec);
    // setMeasuredDimension(1000,200);

}

@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    // super.onDraw(canvas);

    // Create a paint object for us to draw with, and set our drawing color
    // to blue.
    Paint paint = new Paint();
    paint.setColor(Color.RED);
    paint.setAlpha(50);

    Paint paint1 = new Paint();
    paint1.setColor(Color.GREEN);
    paint1.setAlpha(50);

    // draws the rectangle
    if (waveform != null) {
        canvas.drawBitmap(waveform, 0, 0, null);
        if (isEditMode) {
            for (VerbaComment vc : myComments) {

                System.out.println("drawing the comment... start: "
                        + vc.start + " end: " + vc.end);

                canvas.drawRect(vc.start, canvas.getHeight(), vc.end, 0,
                        paint);
            }
        }

        System.out.println("waveform width, height:" + waveform.getWidth()
                + " x " + waveform.getHeight());
    } else {
        canvas.drawRect(0, 0, 50, 50, paint);
    }

    // mImageView.setImageBitmap(map);
}



@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (isEditMode) {
            touchCount++;
            // lastTouch_x=(int)event.getX();
            // System.out.println("onTouch; touchCount: "+touchCount+" lasttouch_x: "+lastTouch_x);
            if (touchCount % 2 == 0) {
                VerbaComment temp = new VerbaComment(lastTouch_x,
                        (int) event.getX());
                myComments.add(temp);

            } else {
                lastTouch_x = (int) event.getX();
                System.out.println("onTouch; touchCount: " + touchCount
                        + " lasttouch_x: " + lastTouch_x);

            }
        }
    }

    invalidate();
    return true;
}

 }

这是活动:

public class AudioPlayerActivity extends Activity{
    // Hardcoded parameters for the Verba demo server
        private static final String ServerURL = "http://demo.verba.com";
        private static final String MediaPath = "C:\\Program%20Files\\Verba\\media\\";

        // Player user interface elements
        private Button mBtnPlay;
        private WaveFormSurfaceView mImageView;

        //private ImageView mImageView;
        private SeekBar mSeekBar;
        private TextView mCurrentPos;
        private TextView mEndPos;

        private Bitmap originalWaveForm;





        // THIS IS THE MEDIAPLAYER (has no UI, only loads and plays the audio)
        private MediaPlayer mMediaPlayer;

        // HTTP URL for the audio waveform PNG picture
        private String getWaveformURL(String pCallURL) {
            return ServerURL + ":8089/a?" + MediaPath + pCallURL
                    + "?10000200240240240123023048240240240240240240";
        }

        // HTTP URL for the audio transcoded to MP3 format
        private String getMediaURL(String pCallURL) {
            return ServerURL + ":10100/getMedia?file=" + MediaPath + pCallURL
                    + "&format=mp3";
        }

        // Downloads the waveform image outside of the main GU thread
        private class DownloadWaveformTask extends AsyncTask<String, Void, Bitmap> {

            @Override
            protected Bitmap doInBackground(String... urls) {
                try {
                    return BitmapFactory.decodeStream((InputStream) new URL(
                            getWaveformURL(myCallURL))
                            .getContent());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap result) {
                try {
                    Thread.sleep(50000);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mImageView.setImageBitmap(result);

            }
        }



        // Updates the seekbar outside of the main GU thread, started only after
        // MediaPlayer exists
        private class SeekBarUpdater extends Thread {
            float p=0;
            @Override
            public void run() {
                final SimpleDateFormat tf = new SimpleDateFormat("mm:ss.SSS");
                final Calendar calendar = Calendar.getInstance();

                int currentPosition = 0;
                final int total = mMediaPlayer.getDuration(); // //returns in msec,
                                                                // final because we
                                                                // will use in the
                                                                // runnable

                // UI update must happen on the UI thread, so we post our actions
                // there in a runnable
                mCurrentPos.post(new Runnable() {
                    public void run() {
                        calendar.setTimeInMillis(total);
                        mEndPos.setText(tf.format(calendar.getTime()));
                        mSeekBar.setMax(total);
                    }
                });

                while (mMediaPlayer != null && currentPosition < total) {
                    try {
                        Thread.sleep(50);
                        currentPosition = mMediaPlayer.getCurrentPosition(); // returns
                                                                                // in
                                                                                // msec
                    } catch (InterruptedException e) {
                        return;
                    } catch (Exception e) {
                        return;
                    }

                    // we are roughly adjusting for delays due to the thread
                    // communication
                    // currentPosition -= 100;
                    // if (currentPosition < 0 ) currentPosition = 0;
                    final int currPosition = currentPosition; // final because we
                                                                // will use in the
                                                                // runnable

                    p=(float)currentPosition/(float)total;

                    // UI update must happen on the UI thread, so we post our
                    // actions there in a runnable
                    mCurrentPos.post(new Runnable() {
                        public void run() {
                            calendar.setTimeInMillis(currPosition);
                            mCurrentPos.setText(tf.format(calendar.getTime()));
                            mSeekBar.setProgress(currPosition);
                            System.out.println("pecent="+p);
                            drawRectOnWaveForm(p);
                        }
                    });

                }
            }
        }




        String myCallURL;

        /**
         * Create a new instance of MyFragment that will be initialized
         * with the given arguments.
         */
        static MediaPlayerFragment newInstance(CharSequence url) {
            MediaPlayerFragment f = new MediaPlayerFragment();
            Bundle args = new Bundle();
            args.putCharSequence("call_url", url);
            f.setArguments(args);
            return f;
        }

        /**
         * During creation, if arguments have been supplied to the fragment
         * then parse those out.
         */
        @Override 
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Bundle extras = getIntent().getExtras();
            if (extras != null) {
                String value1 = extras.getString("call_url");
                if(value1!=null){
                    myCallURL=value1;
                }
            }

            DownloadWaveformTask task = new DownloadWaveformTask();
            task.execute();

            setContentView(R.layout.mediaplayer);
            System.out.println("AudioPlayerActivity setContentView, mycallurl: "+myCallURL);



            // part of the player UI
            //mImageView = (ImageView) findViewById(R.id.imageView);
            mImageView = (WaveFormSurfaceView) findViewById(R.id.imageView);
            mBtnPlay = (Button) findViewById(R.id.playButton);
            mSeekBar = (SeekBar) findViewById(R.id.seekBar);
            mCurrentPos = (TextView) findViewById(R.id.currentPos);
            mEndPos = (TextView) findViewById(R.id.endPos);
            ToggleButton tb=(ToggleButton) findViewById(R.id.toggleButton1);

            tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    // TODO Auto-generated method stub
                    if(isChecked){
                        mImageView.isEditMode=true;
                        mImageView.invalidate();
                    }else{
                        mImageView.isEditMode=false;
                        mImageView.invalidate();
                    }


                }
            });





            //
            mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {
                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                }

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    if (fromUser) {
                        // we only update the player if the change comes from a user
                        // action
                        mMediaPlayer.seekTo(progress);
                    }
                }
            });

            // IMPORTANT
            // - The DownloadWaveformTask part should go into the initialization of
            // the player fragment
            // - currently we are NOT handling currently the end of playback
            // situations, we should
            // - currently we are NOT releasing the MediaPlayer resource, we should
            // when a fragment closes
            mBtnPlay.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    if (mMediaPlayer != null) {
                        if (mMediaPlayer.isPlaying()) {
                            mMediaPlayer.pause();
                            mBtnPlay.setText("Play");
                        } else {
                            mMediaPlayer.start();
                            mBtnPlay.setText("Pause");
                        }
                    } else {


                        mMediaPlayer = new MediaPlayer();
                        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

                        // this updates the seekbar as the buffering happens
                        mMediaPlayer
                                .setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

                                    @Override
                                    public void onBufferingUpdate(MediaPlayer mp,
                                            int percent) {
                                        // TODO Auto-generated method stub
                                        mSeekBar.setSecondaryProgress((int) (mSeekBar
                                                .getMax() * percent / 100));
                                    }
                                });

                        try {
                            final String lMediaURL = getMediaURL(myCallURL);
                            mMediaPlayer.setDataSource(lMediaURL);
                            mMediaPlayer.prepare(); // might take long! (for
                                                    // buffering, etc)
                        } catch (IllegalArgumentException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IllegalStateException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        mMediaPlayer.start();
                        mBtnPlay.setText("Pause");

                        // we start the thread that updates the seekbar, based on
                        // the state of the player
                        SeekBarUpdater thread = new SeekBarUpdater();
                        thread.start();
                    }
                }
            });

            // Close button
            Button closeBtn = (Button) findViewById(R.id.closebtn);
            closeBtn.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    //TODO ha megy a lejátszás, megállítjuk
                    if(mMediaPlayer!=null){
                        if(mMediaPlayer.isPlaying()){
                            mMediaPlayer.stop();
                        }
                    }
                    finish();
                }

            });


        }

}

1 个答案:

答案 0 :(得分:0)

Okz这里有一些我的代码中存在的缺陷

  1. 您正在向android:layout_gravity提供SeekBar =“中心”,另一方面,您正在为其提供match_parent属性
  2. 为什么要将fill_parent提供给id/tableRow3
  3. 并尝试使用fill_parent代替匹配_parent,因为fill_parent是后缀兼容的